Author: ssmalamb
Date: 2007-02-14 12:23:20 +0100 (Wed, 14 Feb 2007)
New Revision: 4437
Added:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/VehicleSearchCommand.java
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/VehicleSearchConfiguration.java
Modified:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/BlocketSearchCommand.java
Log:
Added vehicle enrichment plus changed blocket enrichment
Modified:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
===================================================================
---
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
2007-02-14 10:48:30 UTC (rev 4436)
+++
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
2007-02-14 11:23:20 UTC (rev 4437)
@@ -1,11 +1,30 @@
// Copyright (2006) Schibsted Søk AS
package no.schibstedsok.searchportal.mode;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
import no.schibstedsok.commons.ioc.BaseContext;
import no.schibstedsok.commons.ioc.ContextWrapper;
import no.schibstedsok.searchportal.InfrastructureException;
import no.schibstedsok.searchportal.mode.config.AbstractSearchConfiguration;
import
no.schibstedsok.searchportal.mode.config.AbstractYahooSearchConfiguration;
+import
no.schibstedsok.searchportal.mode.config.AdvancedFastSearchConfiguration;
import
no.schibstedsok.searchportal.mode.config.BlendingNewsSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.BlocketSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.BlogSearchConfiguration;
@@ -30,20 +49,20 @@
import no.schibstedsok.searchportal.mode.config.StaticSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.StockSearchConfiguration;
import
no.schibstedsok.searchportal.mode.config.StormWeatherSearchConfiguration;
+import no.schibstedsok.searchportal.mode.config.TvEnrichSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.TvSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.TvWaitSearchConfiguration;
+import no.schibstedsok.searchportal.mode.config.VehicleSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.WebSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.WhiteSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.YahooIdpSearchConfiguration;
+import no.schibstedsok.searchportal.mode.config.YahooMediaSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.YellowGeoSearchConfiguration;
import no.schibstedsok.searchportal.mode.config.YellowSearchConfiguration;
-import
no.schibstedsok.searchportal.mode.config.AdvancedFastSearchConfiguration;
-import no.schibstedsok.searchportal.mode.config.YahooMediaSearchConfiguration;
import
no.schibstedsok.searchportal.mode.executor.ParallelSearchCommandExecutor;
import no.schibstedsok.searchportal.mode.executor.SearchCommandExecutor;
import
no.schibstedsok.searchportal.mode.executor.SequentialSearchCommandExecutor;
import no.schibstedsok.searchportal.query.transform.QueryTransformer;
-import
no.schibstedsok.searchportal.query.transform.CatalogueInfopageQueryTransformer;
import no.schibstedsok.searchportal.result.Navigator;
import no.schibstedsok.searchportal.result.handler.AddDocCountModifier;
import no.schibstedsok.searchportal.result.handler.AgeCalculatorResultHandler;
@@ -83,27 +102,12 @@
import no.schibstedsok.searchportal.view.output.TextOutputResultHandler;
import no.schibstedsok.searchportal.view.output.VelocityResultHandler;
import no.schibstedsok.searchportal.view.output.XmlOutputResultHandler;
+
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Properties;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
-import java.text.MessageFormat;
-import java.util.Collections;
-import no.schibstedsok.searchportal.mode.config.TvEnrichSearchConfiguration;
/**
* @author <a href="mailto:[EMAIL PROTECTED]>mick</a>
@@ -244,12 +248,12 @@
}
// Package protected ---------------------------------------------
-
+
/* Test use it. **/
Map<String,SearchMode> getModes(){
-
+
return Collections.unmodifiableMap(modes);
- }
+ }
// Protected -----------------------------------------------------
@@ -267,21 +271,21 @@
// loop through modes.
final NodeList modeList = root.getElementsByTagName("mode");
-
+
for (int i = 0; i < modeList.getLength(); ++i) {
final Element modeE = (Element) modeList.item(i);
final String id = modeE.getAttribute("id");
-
+
LOG.info(INFO_PARSING_MODE + modeE.getLocalName() + " " + id);
final SearchMode inherit =
getMode(modeE.getAttribute("inherit"));
-
+
final SearchMode mode = new SearchMode(inherit);
-
+
mode.setId(id);
mode.setExecutor(parseExecutor(modeE.getAttribute("executor"),
inherit != null ? inherit.getExecutor() : new
SequentialSearchCommandExecutor()));
fillBeanProperty(mode, inherit, "analysis", ParseType.Boolean,
modeE, "false");
-
+
// setup new commands list for this mode
final Map<String,SearchConfiguration> modesCommands = new
HashMap<String,SearchConfiguration>();
try{
@@ -295,10 +299,10 @@
for(CommandTypes commandType : CommandTypes.values()){
final NodeList commandsList =
modeE.getElementsByTagName(commandType.getXmlName());
-
+
for (int j = 0; j < commandsList.getLength(); ++j) {
final Element commandE = (Element)
commandsList.item(j);
-
+
final SearchConfiguration sc =
commandType.parseSearchConfiguration(context, commandE, mode);
modesCommands.put(sc.getName(), sc);
mode.addSearchConfiguration(sc);
@@ -373,6 +377,7 @@
BLOG_COMMAND(BlogSearchConfiguration.class),
PRISJAKT_COMMAND(PrisjaktSearchConfiguration.class),
BLOCKET_COMMAND(BlocketSearchConfiguration.class),
+ VEHICLE_COMMAND(VehicleSearchConfiguration.class),
CATALOGUE_COMMAND(CatalogueSearchConfiguration.class),
CATALOGUE_ADS_COMMAND(CatalogueAdsSearchConfiguration.class),
HITTAMAP_COMMAND(HittaMapSearchConfiguration.class);
@@ -475,7 +480,7 @@
fillBeanProperty(sc, inherit, "filtertype",
ParseType.String , commandE, "");
fillBeanProperty(sc, inherit, "ignoreNavigation",
ParseType.Boolean , commandE, "false");
fillBeanProperty(sc, inherit, "offensiveScoreLimit",
ParseType.Int , commandE, "-1");
- fillBeanProperty(sc, inherit, "qtPipeline",
ParseType.String , commandE, "");
+ fillBeanProperty(sc, inherit, "qtPipeline",
ParseType.String , commandE, "");
fillBeanProperty(sc, inherit, "queryServerUrl",
ParseType.String , commandE, "");
fillBeanProperty(sc, inherit, "relevantQueries",
ParseType.Boolean , commandE, "false");
fillBeanProperty(sc, inherit, "sortBy", ParseType.String ,
commandE, "");
@@ -518,7 +523,7 @@
fillBeanProperty(sc, inherit, "collapsingEnabled",
ParseType.Boolean, commandE, "false");
fillBeanProperty(sc, inherit, "expansionEnabled",
ParseType.Boolean, commandE, "false");
fillBeanProperty(sc, inherit, "qtPipeline",
ParseType.String , commandE, "");
- fillBeanProperty(sc, inherit, "queryServer",
ParseType.String , commandE, "");
+ fillBeanProperty(sc, inherit, "queryServer",
ParseType.String , commandE, "");
if (null != esc.getQueryServer() &&
esc.getQueryServer().startsWith("http://")) {
throw new
IllegalArgumentException(ERR_FAST_EPS_QR_SERVER + esc.getQueryServer());
@@ -550,43 +555,68 @@
fillBeanProperty(sc, inherit, "catalog", ParseType.String
, commandE, "");
fillBeanProperty(sc, inherit, "key", ParseType.String ,
commandE, "");
}
-
+
if(sc instanceof PrisjaktSearchConfiguration){
-
-
+
+
}
-
+
if(sc instanceof BlocketSearchConfiguration)
{
- final BlocketSearchConfiguration bsc =
(BlocketSearchConfiguration)sc;
-
-
- Locale locale = new Locale("sv","SE");
+ final BlocketSearchConfiguration bsc =
(BlocketSearchConfiguration)sc;
+ /**
+ * Läser in blocket specifika properties.
+ */
+ final Map<String,String> blocketmap = new
HashMap<String,String>();
+ Properties props = new Properties();
+ PropertiesLoader loader=
UrlResourceLoader.newPropertiesLoader(cxt, bsc.getBlocketConfigFileName(),
props);
+ loader.abut();
+ /**
+ * Lägger properties i en Map för snabbare åtkomst.
+ */
+ for (Object value : props.values()) {
+ final String field[] = ((String)value).split(";");
+ if (field.length == 2) {
+ blocketmap.put(field[0], field[1]);
+ }
+ }
+ bsc.setBlocketMap(blocketmap);
+ }
- /**
- * Läser in blocket specifika properties.
- */
- final Map<String,String> blocketmap = new
HashMap<String,String>();
- Properties props = new Properties();
- PropertiesLoader loader=
UrlResourceLoader.newPropertiesLoader(cxt, bsc.getBlocketConfigFileName(),
props);
- loader.abut();
- /**
- * Lägger properties i en Map för snabbare åtkomst.
- */
- for(Map.Entry<Object,Object> entry : props.entrySet())
- {
+ if(sc instanceof VehicleSearchConfiguration) {
+ final VehicleSearchConfiguration vsc =
(VehicleSearchConfiguration)sc;
+ /**
+ * Läser in Vehicle search specifika properties.
+ */
+ final Set<String> accessoriesSet = new HashSet<String>();
+ Properties props = new Properties();
+ PropertiesLoader loader=
UrlResourceLoader.newPropertiesLoader(cxt, vsc.getAccessoriesFileName(), props);
+ loader.abut();
- final String key =
((String)entry.getKey()).toLowerCase(locale);
- final String value =
((String)entry.getValue()).toLowerCase(locale);
- blocketmap.put(key, value);
-
+ final Map<String,String> carMap = new
HashMap<String,String>();
+ Properties carProps = new Properties();
+ PropertiesLoader carLoader=
UrlResourceLoader.newPropertiesLoader(cxt, vsc.getCarsPropertiesFileName(),
carProps);
+ carLoader.abut();
+
+ /**
+ * Lägger properties i ett Set och en Map för snabbare
åtkomst.
+ */
+ for (Object value : props.values()) {
+ accessoriesSet.add((String)value);
+ }
+ vsc.setAccessoriesSet(accessoriesSet);
+
+ for (Object value : carProps.values()) {
+ final String field[] = ((String)value).split(":"); //
Ignore the key, the value is key/value tuple, ugly but it works,
+ if (field.length == 2) { //
example: property1963=volvo p 1800:volvo;p 1800
+ carMap.put(field[0], field[1]);
+ }
}
-
- bsc.setBlocketMap(blocketmap);
-
- }
-
+ vsc.setCarsMap(carMap);
+
+ }
+
if(sc instanceof AbstractYahooSearchConfiguration){
fillBeanProperty(sc, inherit, "encoding", ParseType.String
, commandE, "");
fillBeanProperty(sc, inherit, "partnerId",
ParseType.String , commandE, "");
@@ -596,9 +626,9 @@
}
if (sc instanceof YahooMediaSearchConfiguration) {
- fillBeanProperty(sc, inherit, "catalog", ParseType.String,
commandE,
+ fillBeanProperty(sc, inherit, "catalog", ParseType.String,
commandE,
YahooMediaSearchConfiguration.DEFAULT_CATALOG);
- fillBeanProperty(sc, inherit, "ocr", ParseType.String,
commandE,
+ fillBeanProperty(sc, inherit, "ocr", ParseType.String,
commandE,
YahooMediaSearchConfiguration.DEFAULT_OCR);
fillBeanProperty(sc, inherit, "site", ParseType.String,
commandE, "");
}
@@ -622,7 +652,7 @@
fillBeanProperty(sc, inherit, "unique", ParseType.String ,
commandE, "");
}
if(sc instanceof PicSearchConfiguration){
-
+
fillBeanProperty(sc, inherit, "queryServerHost",
ParseType.String , commandE, "");
fillBeanProperty(sc, inherit, "queryServerPort",
ParseType.String , commandE, "");
fillBeanProperty(sc, inherit, "country", ParseType.String
, commandE, "no");
@@ -690,19 +720,19 @@
tssc.setResultsToFetch(Integer.parseInt(commandE.getAttribute("results-to-fetch")));
}
-
+
if (sc instanceof TvWaitSearchConfiguration) {
final TvWaitSearchConfiguration twsc =
(TvWaitSearchConfiguration) sc;
fillBeanProperty(twsc, inherit, "index", ParseType.Int,
commandE, "0");
fillBeanProperty(twsc, inherit, "waitOn",
ParseType.String, commandE, null);
fillBeanProperty(twsc, inherit, "useMyChannels",
ParseType.Boolean, commandE, "false");
}
-
+
if (sc instanceof TvEnrichSearchConfiguration) {
final TvEnrichSearchConfiguration tesc =
(TvEnrichSearchConfiguration) sc;
fillBeanProperty(tesc, inherit, "waitOn",
ParseType.String, commandE, null);
}
-
+
if(sc instanceof CatalogueSearchConfiguration){
final CatalogueSearchConfiguration csc =
(CatalogueSearchConfiguration) sc;
fillBeanProperty(csc, inherit, "queryParameterWhere",
ParseType.String , commandE, "");
@@ -710,10 +740,10 @@
if(sc instanceof CatalogueAdsSearchConfiguration){
final CatalogueAdsSearchConfiguration casc =
(CatalogueAdsSearchConfiguration) sc;
fillBeanProperty(casc, inherit, "queryParameterWhere",
ParseType.String , commandE, "");
- }
-
-
+ }
+
+
// query transformers
NodeList qtNodeList =
commandE.getElementsByTagName("query-transformers");
final Element qtRootElement = (Element) qtNodeList.item(0);
@@ -791,7 +821,7 @@
}
config = configs.get(id);
m = m.getParentSearchMode();
-
+
}while(config == null && m != null);
return config;
@@ -807,7 +837,7 @@
final Element navE = (Element)child;
final String id = navE.getAttribute("id");
final String name = navE.getAttribute("name");
- final String sortAttr = navE.getAttribute("sort") != null
&& navE.getAttribute("sort").length() > 0
+ final String sortAttr = navE.getAttribute("sort") != null
&& navE.getAttribute("sort").length() > 0
? navE.getAttribute("sort").toUpperCase() :
"COUNT";
LOG.info(INFO_PARSING_NAVIGATOR + id + " [" + name + "]");
final Navigator.Sort sort =
Navigator.Sort.valueOf(sortAttr);
@@ -836,17 +866,17 @@
private static final class QueryTransformerFactory {
private QueryTransformerFactory(){}
-
+
static boolean supported(final String xmlName){
-
+
return null != findClass(xmlName);
}
static QueryTransformer parseQueryTransformer(final Element qt){
-
+
final String xmlName = qt.getTagName();
LOG.info(INFO_PARSING_QUERY_TRANSFORMER + xmlName);
-
+
try {
return
findClass(xmlName).newInstance().readQueryTransformer(qt);
@@ -856,18 +886,18 @@
throw new InfrastructureException(ex);
}
}
-
+
private static Class<? extends QueryTransformer> findClass(final
String xmlName){
-
+
Class clazz = null;
final String bName = xmlToBeanName(xmlName);
final String className = Character.toUpperCase(bName.charAt(0)) +
bName.substring(1, bName.length());
try{
clazz = (Class<? extends QueryTransformer>) Class.forName(
"no.schibstedsok.searchportal.query.transform."
- + className
+ + className
+ "QueryTransformer");
-
+
}catch(ClassNotFoundException cnfe){
LOG.error(cnfe.getMessage(), cnfe);
}
@@ -927,10 +957,10 @@
LOG.info(INFO_PARSING_RESULT_HANDLER + xmlName);
final ResultHandler handler = clazz.newInstance();
switch(this){
- case CATALOGUE:
+ case CATALOGUE:
final CatalogueResultHandler crh =
(CatalogueResultHandler) handler;
break;
-
+
case ADD_DOC_COUNT:
final AddDocCountModifier adc = (AddDocCountModifier)
handler;
adc.setModifierName(rh.getAttribute("modifier"));
Modified:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/BlocketSearchCommand.java
===================================================================
---
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/BlocketSearchCommand.java
2007-02-14 10:48:30 UTC (rev 4436)
+++
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/BlocketSearchCommand.java
2007-02-14 11:23:20 UTC (rev 4437)
@@ -5,19 +5,23 @@
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.util.Map;
+
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.holders.LongHolder;
import javax.xml.rpc.holders.StringHolder;
-import org.apache.commons.lang.StringUtils;
-import org.apache.log4j.Logger;
-import se.blocket.www2.search.SearchLocator;
-import se.blocket.www2.search.SearchPortType;
+
import no.schibstedsok.searchportal.InfrastructureException;
import no.schibstedsok.searchportal.mode.config.BlocketSearchConfiguration;
import no.schibstedsok.searchportal.result.BasicSearchResult;
import no.schibstedsok.searchportal.result.SearchResult;
+
import org.apache.axis.client.Stub;
+import org.apache.log4j.Logger;
+import se.blocket.www2.search.SearchLocator;
+import se.blocket.www2.search.SearchPortType;
+
+
public final class BlocketSearchCommand extends
AbstractWebServiceSearchCommand {
private static final Logger LOG =
Logger.getLogger(BlocketSearchCommand.class);
@@ -26,16 +30,14 @@
private static final String ERR_FAILED_ENCODE_BLOCKET = "Failed to encode
Blocket search query";
- public BlocketSearchCommand(final Context cxt, final Map<String, Object>
parameters) {
-
+ public BlocketSearchCommand(final Context cxt, final Map<String, Object>
parameters) {
super(cxt, parameters);
}
@Override
public SearchResult execute() {
- final BlocketSearchConfiguration bsc
- = (BlocketSearchConfiguration)
context.getSearchConfiguration();
+ BlocketSearchConfiguration bsc = (BlocketSearchConfiguration)
context.getSearchConfiguration();
final SearchLocator service = new SearchLocator();
final SearchResult result = new BasicSearchResult(this);
@@ -45,40 +47,44 @@
((Stub)port).setTimeout(1000);
final String query = getTransformedQuery();
- /*Trimmar frågan så den har samma format som i blocket.properties,
- dvs inga å ä ö eller mellanslag*/
- final String trimQ = StringUtils.deleteWhitespace(query);
- final String lowerCase= trimQ.toLowerCase();
- final String trimQ1 = StringUtils.replace(lowerCase, "\u00E5",
"a");
- final String trimQ2 = StringUtils.replace(trimQ1, "\u00E4", "a");
- final String trimQ3 = StringUtils.replace(trimQ2, "\u00F6", "o");
- /*Innhåller kategorimappningar för blocket*/
- final Map top100BlocketMap = bsc.getBlocketMap();
- final String categoryIndex = (String) top100BlocketMap.get(trimQ3);
- /*Svars parametrar ifrån blocket*/
- final LongHolder lholder = new LongHolder();
- final StringHolder sholder = new StringHolder();
+ /* Innhåller kategorimappningar för blocket */
+ final String category = (String)
bsc.getBlocketMap().get(query.toLowerCase());
+ String categoryFields[] = null;
+ String categoryName = ""; // Dont use category name for
"Alla kategorier"
+ String categoryIndex = "0"; // 0 = Alla kategorier,
constant for this?
+ if (category != null) {
+ categoryFields = category.split(":");
+ if (categoryFields.length == 2) {
+ categoryIndex = categoryFields[0];
+ StringBuilder sb = new StringBuilder("i "); //
1042 annonsträffar i TV-spel & PC-spel på WOW ELLER 834 annonsträffar på LG
+ sb.append(categoryFields[1].replace("&","&"));
+ categoryName = sb.toString();
+ }
+ }
+
+ /* Svarsparametrar ifrån blocket */
+ LongHolder lholder = new LongHolder();
+ StringHolder sholder = new StringHolder();
+
LOG.debug("Executing blocket search command with searchquery: " +
query);
- if (categoryIndex != null) {
- port.search(query, Integer.parseInt(categoryIndex),"",
lholder, sholder);
+ if (category != null) {
+ port.search(query, Integer.parseInt(categoryIndex), "",
lholder, sholder);
}
final String nads = Long.toString(lholder.value);
if ((nads != null) && (!nads.equalsIgnoreCase("0"))) {
-
result.addField("searchquery", query);
+ result.addField("categoryName", categoryName);
result.addField("numberofads", nads);
- final String blocketBackURL = URLEncoder.encode(sholder.value,
"iso-8859-1");
- result.addField("blocketbackurl", blocketBackURL);
-
+ result.addField("blocketbackurl",
URLEncoder.encode(sholder.value, "iso-8859-1"));
result.setHitCount(Integer.parseInt(nads));
-
}
+ }
- }catch (ServiceException se) {
+ catch (ServiceException se) {
LOG.error(ERR_FAILED_BLOCKET_SEARCH, se);
throw new InfrastructureException(se);
} catch (MalformedURLException murle) {
@@ -90,8 +96,6 @@
} catch (UnsupportedEncodingException usee) {
LOG.error(ERR_FAILED_ENCODE_BLOCKET, usee);
}
-
return result;
}
-
}
Added:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/VehicleSearchCommand.java
===================================================================
---
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/VehicleSearchCommand.java
(rev 0)
+++
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/VehicleSearchCommand.java
2007-02-14 11:23:20 UTC (rev 4437)
@@ -0,0 +1,183 @@
+package no.schibstedsok.searchportal.mode.command;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URLEncoder;
+import java.rmi.RemoteException;
+import java.util.Map;
+
+import javax.xml.rpc.ServiceException;
+import javax.xml.rpc.holders.LongHolder;
+import javax.xml.rpc.holders.StringHolder;
+
+import no.schibstedsok.searchportal.InfrastructureException;
+import no.schibstedsok.searchportal.mode.config.VehicleSearchConfiguration;
+import no.schibstedsok.searchportal.result.BasicSearchResult;
+import no.schibstedsok.searchportal.result.SearchResult;
+
+import org.apache.axis.client.Stub;
+import org.apache.log4j.Logger;
+import org.jdom.Document;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+
+import se.blocket.www2.search.SearchLocator;
+import se.blocket.www2.search.SearchPortType;
+
+public class VehicleSearchCommand extends AbstractWebServiceSearchCommand {
+
+ private static final Logger LOG =
Logger.getLogger(VehicleSearchCommand.class);
+
+ private static final String ERR_FAILED_BLOCKET_SEARCH = "Failed Vehicle
search command";
+
+ private static final String ERR_FAILED_ENCODE_BLOCKET = "Failed to encode
Vehicle search query";
+
+ private final static String BYTBIL_BASE_URL =
"http://www.bytbil.com/sellers/sesam/sesam.cgi?uid=4A35c0023F&brand=%s&model=%s";
+
+ private final static int BLOCKET_CAR_CATEGORY = 4;
+
+ private final static int BLOCKET_CAR_ACCESSORIES_CATEGORY = 5;
+
+ private final static String BLOCKET_PRIVATE_ADS_TYPE = "p";
+
+ private final static String BLOCKET_ALL_ADS_TYPE = "";
+
+ public VehicleSearchCommand(final Context cxt, final Map<String, Object>
parameters) {
+ super(cxt, parameters);
+ }
+
+ @Override
+ public SearchResult execute() {
+
+ VehicleSearchConfiguration vsc = (VehicleSearchConfiguration)
context.getSearchConfiguration();
+
+ final SearchLocator service = new SearchLocator();
+ final SearchResult result = new BasicSearchResult(this);
+
+ try {
+
+ final SearchPortType port = service.getsearchPort(new
java.net.URL(service.getsearchPortAddress()));
+
+ String query = getTransformedQuery();
+ /* Svars parametrar ifrån blocket */
+ LongHolder lholder = new LongHolder();
+ StringHolder sholder = new StringHolder();
+ Map carsMap = vsc.getCarsMap();
+
+ LOG.debug("Executing vehicle search command with searchquery: " +
query);
+
+ // Blocket cars
+ String nads = "0";
+ if (carsMap.get(query.toLowerCase()) != null) {
+ port.search(query, BLOCKET_CAR_CATEGORY,
BLOCKET_PRIVATE_ADS_TYPE, lholder, sholder);
+ ((Stub)port).setTimeout(1000);
+ nads = Long.toString(lholder.value);
+ if ((nads != null) && (!nads.equals("0"))) {
+ result.addField("searchquery", query);
+ String blocketBackURL = URLEncoder.encode(sholder.value,
"iso-8859-1");
+ result.addField("blocketbackurl", blocketBackURL);
+ }
+ }
+ result.addField("numberofads", nads);
+
+ // bytbil
+ String bbCount = "0";
+ if (carsMap.get(query.toLowerCase()) != null) {
+ SAXBuilder sb = new SAXBuilder();
+ String[] queryParts = ((String)
carsMap.get(query.toLowerCase())).split(";");
+ Document doc=null;
+ if (queryParts.length == 2) {
+ doc = sb.build(String.format(BYTBIL_BASE_URL,
URLEncoder.encode(queryParts[0], "iso-8859-1"),
+ URLEncoder.encode(queryParts[1], "iso-8859-1")));
+ bbCount =
doc.getRootElement().getAttribute("count").getValue();
+ } else if (
((String)carsMap.get(query.toLowerCase())).equals("bytbil")) {
+ queryParts = query.toLowerCase().split(" ");
+ doc = sb.build(String.format(BYTBIL_BASE_URL,
URLEncoder.encode(queryParts[0], "iso-8859-1"),
+ URLEncoder.encode(queryParts[1], "iso-8859-1")));
+ bbCount =
doc.getRootElement().getAttribute("count").getValue();
+ }
+ System.out.println("bbCount: " + bbCount);
+ if (!bbCount.equals("0")) {
+ String bbUrl =
doc.getRootElement().getChildTextTrim("searchquery");
+ result.addField("bytbilbackurl", URLEncoder.encode(bbUrl,
"iso-8859-1"));
+ }
+ }
+ result.addField("bbnumberofads", bbCount);
+
+ boolean blocketContainsAccessories =
vsc.getAccessriesSet().contains(query.toLowerCase());
+ String accNads = "0";
+
+ LOG.debug("Executing car accessories search command with
searchquery: " + query);
+
+ if (blocketContainsAccessories) {
+ LongHolder numberOfAccessories = new LongHolder();
+ StringHolder accUrl = new StringHolder();
+ port.search(query, BLOCKET_CAR_ACCESSORIES_CATEGORY,
BLOCKET_ALL_ADS_TYPE, numberOfAccessories, accUrl);
+ ((Stub)port).setTimeout(1000);
+ accNads = Long.toString(numberOfAccessories.value);
+ if ((accNads != null) && (!accNads.equalsIgnoreCase("0"))) {
+ result.addField("searchquery", query);
+ String accblocketBackURL = URLEncoder.encode(accUrl.value,
"iso-8859-1");
+ result.addField("accblocketbackurl", accblocketBackURL);
+ }
+ }
+ result.addField("accnumberofads", accNads);
+
+ final int totalnumberofads = Integer.parseInt(nads) +
Integer.parseInt(bbCount) + Integer.parseInt(accNads);
+ result.addField("totalnumberofads",
Integer.toString(totalnumberofads));
+ result.setHitCount(totalnumberofads);
+ }
+
+ catch (ServiceException se) {
+ LOG.error(ERR_FAILED_BLOCKET_SEARCH, se);
+ throw new InfrastructureException(se);
+ } catch (MalformedURLException murle) {
+ LOG.error(ERR_FAILED_BLOCKET_SEARCH, murle);
+ throw new InfrastructureException(murle);
+ } catch (RemoteException re) {
+ LOG.error(ERR_FAILED_BLOCKET_SEARCH, re);
+ throw new InfrastructureException(re);
+ } catch (UnsupportedEncodingException usee) {
+ LOG.error(ERR_FAILED_ENCODE_BLOCKET, usee);
+ } catch (JDOMException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return result;
+ }
+
+ public enum VehicleType {
+ CAR(4), MC(6), MOPED(7);
+
+ private final int blocketCategory;
+
+ VehicleType(int blocketCategory) {
+ this.blocketCategory = blocketCategory;
+ }
+
+ public int blocketCategory() {
+ return blocketCategory;
+ }
+
+ };
+
+ public class VehicleProperties {
+ VehicleProperties() {
+ }
+
+ private VehicleType type = null;
+
+ private String brand = "";
+
+ private String model = "";
+ }
+ /**
+ * getBrandFromModel(model) getBrand(str) getModel(str) getType(str)
+ */
+
+}
Added:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/VehicleSearchConfiguration.java
===================================================================
---
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/VehicleSearchConfiguration.java
(rev 0)
+++
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/VehicleSearchConfiguration.java
2007-02-14 11:23:20 UTC (rev 4437)
@@ -0,0 +1,50 @@
+package no.schibstedsok.searchportal.mode.config;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Configuration for the Vehicle Search command.
+ */
+public class VehicleSearchConfiguration extends AbstractSearchConfiguration {
+
+ private Map carsMap;
+
+ private Set accessoriesSet;
+
+// private static final String VEHICLE_CONFIGURATION_FILE =
"blocket.properties";
+ private static final String VEHICLE_ACCESSORIES_PROPERTIES =
"vehicle_accessories.properties";
+ private static final String VEHICLE_CARS_PROPERTIES =
"vehicle_cars.properties";
+
+ /**
+ * Creates a new instance of this search configuration.
+ */
+ public VehicleSearchConfiguration(final SearchConfiguration sc) {
+ super(sc);
+ }
+
+ public String getCarsPropertiesFileName() {
+ return VEHICLE_CARS_PROPERTIES;
+ }
+
+ public Map getCarsMap() {
+ return carsMap;
+ }
+
+ public void setCarsMap(Map bmap) {
+ carsMap = bmap;
+ }
+
+ public String getAccessoriesFileName() {
+ return VEHICLE_ACCESSORIES_PROPERTIES;
+ }
+
+ public Set getAccessriesSet() {
+ return accessoriesSet;
+ }
+
+ public void setAccessoriesSet(Set acc) {
+ accessoriesSet = acc;
+ }
+
+}
_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits