Author: ssmiweve Date: 2007-05-10 12:24:27 +0200 (Thu, 10 May 2007) New Revision: 5048
Modified: trunk/ trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/DateFormattingDirective.java trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/VelocityEngineFactory.java trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractLeafClause.java trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractReflectionVisitor.java trunk/query-transform-control-spi/src/main/java/no/schibstedsok/searchportal/query/transform/NewsMyNewsQueryTransformer.java Log: Merged revisions 272-273 via svnmerge from https://dev.schibstedsok.no/svn/search-config/xml.sesam.no/branches/2.12 ........ r272 | mickw | 2006-04-06 21:02:14 +0200 (Thu, 06 Apr 2006) | 2 lines missing htdocs directory ........ r273 | mickw | 2006-04-06 21:08:21 +0200 (Thu, 06 Apr 2006) | 3 lines avoid printing out org.apache.velocity.runtime.resource.ResourceManager errors. my abuse of the velocity API is generating unneccesary error msgs that mean nothing to us. ........ Property changes on: trunk ___________________________________________________________________ Name: svnmerge-integrated - /branches/2.10:1-4690,4692-4745 /branches/2.11:1-4933 /branches/2.12:1-5035 /branches/2.6:1-3877 /branches/2.7:1-4160 /branches/2.8:1-4446 /branches/2.9:1-4626 /branches/MAP_SEARCHv2:1-4544 + /branches/2.10:1-4690,4692-4745 /branches/2.11:1-4933 /branches/2.12:1-5047 /branches/2.6:1-3877 /branches/2.7:1-4160 /branches/2.8:1-4446 /branches/2.9:1-4626 /branches/MAP_SEARCHv2:1-4544 Modified: trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/DateFormattingDirective.java =================================================================== --- trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/DateFormattingDirective.java 2007-05-10 10:04:23 UTC (rev 5047) +++ trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/DateFormattingDirective.java 2007-05-10 10:24:27 UTC (rev 5048) @@ -11,39 +11,56 @@ package no.schibstedsok.searchportal.view.velocity; import org.apache.log4j.Logger; +import org.apache.velocity.context.InternalContextAdapter; +import org.apache.velocity.exception.MethodInvocationException; +import org.apache.velocity.exception.ParseErrorException; +import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.directive.Directive; +import org.apache.velocity.runtime.parser.Token; import org.apache.velocity.runtime.parser.node.Node; -import org.apache.velocity.runtime.parser.Token; -import org.apache.velocity.context.InternalContextAdapter; -import org.apache.velocity.exception.ResourceNotFoundException; -import org.apache.velocity.exception.ParseErrorException; -import org.apache.velocity.exception.MethodInvocationException; +import java.io.IOException; import java.io.Writer; -import java.io.IOException; -import java.util.Map; -import java.util.HashMap; +import java.text.DateFormatSymbols; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; -import java.text.SimpleDateFormat; +import java.util.TimeZone; /** - * * A velocity directive to format newsnavigator date - * - * Newsdate comes from the fastnavigator in two forms: + * <p/> + * Newsdate comes from the fastnavigator in four forms: * 1. 10-2006 -> oktober 2006 - * 2. 10-24-2006 -> 10. oktober 2006 - * + * 2. 24-10-2006 -> 24. oktober 2006 + * 3. 2006-10 -> oktober 2006 + * 4. 2006-10-24 -> 24. oktober 2006 + * <p/> * if 'newsdateOnly' navigator, we shuold check if the date is today or yesterday - * */ public final class DateFormattingDirective extends Directive { - + private static DateFormatSymbols formatSymbols = new DateFormatSymbols(); + + static { + // Default symbols are all lowercase for "no", so we have to set new ones. + formatSymbols.setMonths(new String[]{"Januar", "Februar", "Mars", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Desember"}); + } + + private static SimpleDateFormat shortFormatter = new SimpleDateFormat("MMMMM yyyy", formatSymbols); + private static SimpleDateFormat longFormatter = new SimpleDateFormat("d. MMMMM yyyy", formatSymbols); + private static final Logger LOG = Logger.getLogger(DateFormattingDirective.class); private static final String NAME = "dateFormatting"; + private static SimpleDateFormat formOneParser = new SimpleDateFormat("MM-yyyy"); + private static SimpleDateFormat formTwoParser = new SimpleDateFormat("dd-MM-yyyy"); + private static SimpleDateFormat formThreeParser = new SimpleDateFormat("yyyy-MM"); + private static SimpleDateFormat formFourParser = new SimpleDateFormat("yyyy-MM-dd"); + + /** * [EMAIL PROTECTED] */ @@ -69,42 +86,18 @@ } final String input = node.jjtGetChild(0).value(context).toString(); - final String navName = node.jjtGetChild(1).value(context).toString(); - - final SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); - Map<String, String> map = new HashMap<String, String>(); - map.put("01", "Januar"); - map.put("02", "Februar"); - map.put("03", "Mars"); - map.put("04", "April"); - map.put("05", "Mai"); - map.put("06", "Juni"); - map.put("07", "Juli"); - map.put("08", "August"); - map.put("09", "September"); - map.put("10", "Oktober"); - map.put("11", "November"); - map.put("12", "Desember"); - - String fDate = ""; - final Date today = new Date(); - long time = today.getTime(); - time -= 24*60*60*1000; - final Date yesterday = new Date(time); - String sToday = formatter.format(today); - String sYesterday = formatter.format(yesterday); - - if (sToday.equals(input) && "newsdateOnly".equals(navName)) - fDate = "I dag"; - else if (sYesterday.equals(input) && "newsdateOnly".equals(navName)) - fDate = "I går"; - else if (input.length() == 7) - fDate = map.get(input.substring(0,2)) + " " + input.substring(3,7); - else if (input.length() == 10) - fDate = input.substring(0, 2) + ". " + map.get(input.substring(3,5)).toLowerCase() + " " + input.substring(6,10); - else - fDate = input; + String navName = null; + if (node.jjtGetNumChildren() > 1) { + navName = node.jjtGetChild(1).value(context).toString(); + } + + String fDate = input; + try { + fDate = formatDate(input, navName); + } catch (ParseException e) { + LOG.error("Could not parse date", e); + } writer.write(fDate); final Token lastToken = node.getLastToken(); @@ -115,5 +108,72 @@ return true; } - + + protected String formatDate(String input, String navName) throws ParseException { + if (input.substring(2, 3).equals("-")) { + // Form one or two + if (input.length() == 10) { + return formatFormTwo(input, "newsdateOnly".equals(navName)); + } else { + return formatFormOne(input); + } + } else { + // From three or four + if (input.length() == 10) { + return formatFormFour(input, "newsdateOnly".equals(navName)); + } else { + return formatFormThree(input); + } + } + } + + protected String formatFormOne(String input) throws ParseException { + // 1. 10-2006 -> oktober 2006 + return shortFormatter.format(formOneParser.parse(input)); + } + + protected String formatFormTwo(String input, boolean newsDateOnly) throws ParseException { + // 2. 24-10-2006 -> 24. oktober 2006 + Date parsedDate = formTwoParser.parse(input); + return longFormat(parsedDate, newsDateOnly); + } + + protected String formatFormThree(String input) throws ParseException { + // 3. 2006-10 -> oktober 2006 + return shortFormatter.format(formThreeParser.parse(input)); + } + + protected String formatFormFour(String input, boolean newsDateOnly) throws ParseException { + Date parsedDate = formFourParser.parse(input); + return longFormat(parsedDate, newsDateOnly); + } + + private String longFormat(Date parsedDate, boolean newsDateOnly) { + if (newsDateOnly) { + if (isToday(parsedDate)) { + return "I dag"; + } else if (isYesterday(parsedDate)) { + return "I går"; + } + } + return longFormatter.format(parsedDate); + } + + private boolean isToday(Date parsedDate) { + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + int todayDay = cal.get(Calendar.DAY_OF_YEAR); + int year = cal.get(Calendar.YEAR); + cal.setTime(parsedDate); + return todayDay == cal.get(Calendar.DAY_OF_YEAR) && year == cal.get(Calendar.YEAR); + } + + private boolean isYesterday(Date parsedDate) { + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + cal.add(Calendar.DAY_OF_YEAR, -1); + int yesterdayDay = cal.get(Calendar.DAY_OF_YEAR); + int year = cal.get(Calendar.YEAR); + cal.setTime(parsedDate); + return yesterdayDay == cal.get(Calendar.DAY_OF_YEAR) && year == cal.get(Calendar.YEAR); + } + } Modified: trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/VelocityEngineFactory.java =================================================================== --- trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/VelocityEngineFactory.java 2007-05-10 10:04:23 UTC (rev 5047) +++ trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/VelocityEngineFactory.java 2007-05-10 10:24:27 UTC (rev 5048) @@ -249,6 +249,7 @@ if(!VELOCITY_DEBUG) { engine.setProperty("url.resource.loader.cache", "true"); engine.setProperty("url.resource.loader.modificationCheckInterval", "300"); // 5 minutes + engine.setProperty(Velocity.RESOURCE_MANAGER_DEFAULTCACHE_SIZE, "1000"); } engine.setProperty(Site.NAME_KEY, site); Modified: trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractLeafClause.java =================================================================== --- trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractLeafClause.java 2007-05-10 10:04:23 UTC (rev 5047) +++ trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractLeafClause.java 2007-05-10 10:24:27 UTC (rev 5048) @@ -62,7 +62,7 @@ // important that the key argument is unique to this object. final String key = field != null ? field + ':' + term - : term; + : null != term ? term : ""; // check weak reference cache of immutable wordClauses here. // no need to synchronise, no big lost if duplicate identical objects are created and added over each other @@ -83,7 +83,7 @@ ); // use the constructor... clause = constructor.newInstance( - term, + null != term ? term : "", field, engine.getState().getKnownPredicates(), engine.getState().getPossiblePredicates() Modified: trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractReflectionVisitor.java =================================================================== --- trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractReflectionVisitor.java 2007-05-10 10:04:23 UTC (rev 5047) +++ trunk/query-api/src/main/java/no/schibstedsok/searchportal/query/parser/AbstractReflectionVisitor.java 2007-05-10 10:24:27 UTC (rev 5048) @@ -63,10 +63,17 @@ } catch (IllegalArgumentException ex) { LOG.error(ERR_FAILED_TO_VISIT + clause, ex); + } catch (InvocationTargetException ex) { LOG.error(ERR_FAILED_TO_VISIT + clause, ex); + // IllegalArgumentException are often mean an underlying exception + for (Throwable t = ex; t != null; t = t.getCause()) { + LOG.error(t.getMessage(), t); + } + } catch (IllegalAccessException ex) { LOG.error(ERR_FAILED_TO_VISIT + clause, ex); + } finally { method.setAccessible(false); } Modified: trunk/query-transform-control-spi/src/main/java/no/schibstedsok/searchportal/query/transform/NewsMyNewsQueryTransformer.java =================================================================== --- trunk/query-transform-control-spi/src/main/java/no/schibstedsok/searchportal/query/transform/NewsMyNewsQueryTransformer.java 2007-05-10 10:04:23 UTC (rev 5047) +++ trunk/query-transform-control-spi/src/main/java/no/schibstedsok/searchportal/query/transform/NewsMyNewsQueryTransformer.java 2007-05-10 10:24:27 UTC (rev 5048) @@ -1,3 +1,4 @@ +// Copyright (2007) Schibsted Søk AS package no.schibstedsok.searchportal.query.transform; import no.schibstedsok.searchportal.query.Clause; @@ -14,46 +15,75 @@ * <p/> * <b>Note:</b> This queryTransformer ignores all earlier transforms on the query. All transforms to the resulting * query should be done after this. + * + * @author geir + * @version $Id$ */ -public class NewsMyNewsQueryTransformer extends AbstractQueryTransformer { +public final class NewsMyNewsQueryTransformer extends AbstractQueryTransformer { + private static final Logger LOG = Logger.getLogger(NewsMyNewsQueryTransformer.class); private static final Pattern queryPattern = Pattern.compile("(?:\\A|\\|)([^\\|]+)\\:{2}([^\\|]+)\\|?"); - private NewsMyNewsQueryTransformerConfig config; + private final NewsMyNewsQueryTransformerConfig config; - public NewsMyNewsQueryTransformer(QueryTransformerConfig config) { + /** + * + * @param config + */ + public NewsMyNewsQueryTransformer(final QueryTransformerConfig config) { this.config = (NewsMyNewsQueryTransformerConfig) config; } + /** + * + * @param clause + */ public void visitImpl(final Clause clause) { - String myNews = (String) getContext().getDataModel().getJunkYard().getValue(config.getQueryParameter()); + + final String myNews = (String) getContext().getDataModel().getJunkYard().getValue(config.getQueryParameter()); + + // FIXME use instead the following + //final String myNews + // = (String) getContext().getDataModel().getParameters().getValue(config.getQueryParameter()).getString(); + LOG.debug("Transforming query according to query = " + myNews); final String transformedQuery = transformQuery(myNews); + if (transformedQuery != null) { - LOG.debug("New query is: '" + transformedQuery + "'"); + LOG.debug("New query is: '" + transformedQuery + '\''); + for (Clause keyClause : getContext().getTransformedTerms().keySet()) { getContext().getTransformedTerms().put(keyClause, ""); } + if (transformedQuery.length() > 0) { getContext().getTransformedTerms().put(getContext().getQuery().getFirstLeafClause(), transformedQuery); } } } + /** + * + * @return + */ protected int getOffset() { - int offset = 0; - if (getContext().getDataModel().getJunkYard().getValue("offset") != null) { - offset = Integer.parseInt((String) getContext().getDataModel().getJunkYard().getValue("offset")); - } - return offset; + + return null != getContext().getDataModel().getJunkYard().getValue("offset") + ? Integer.parseInt((String) getContext().getDataModel().getJunkYard().getValue("offset")) + : 0; } - private String transformQuery(String myNews) { + private String transformQuery(final String myNews) { + + final StringBuilder newQuery = new StringBuilder(); + if (myNews != null && myNews.length() > 0) { + final Matcher matcher = queryPattern.matcher(myNews); if (config.getPosition() == -1) { + LOG.debug("No position. Appending all matches."); - final StringBuilder newQuery = new StringBuilder(); + while (matcher.find()) { if (matcher.group(2).equals(config.getType())) { if (newQuery.length() == 0) { @@ -69,30 +99,38 @@ if (newQuery.length() > 0 && config.getFilterField() != null) { newQuery.append("))"); } - return newQuery.toString(); + } else { + LOG.debug("Position is: " + config.getPosition()); int curPos = 0; - int offset = getOffset(); - int pos = config.getPosition() + offset; + final int offset = getOffset(); + final int pos = config.getPosition() + offset; + while (matcher.find() && curPos < pos) { // Just searching for the correct match. curPos++; } - LOG.debug("Group at pos: " + pos + " is " + matcher.group(0) + ", looking for " + config.getType()); + + try{ + // YUK! any way to do this and avoid having to wrap in try-catch?? XXX + LOG.debug("Group at pos: " + pos + " is " + matcher.group(0) + ", looking for " + config.getType()); + + }catch(IllegalStateException ise){ + LOG.trace(ise.getMessage(), ise); + } + if (matcher.groupCount() > 0 && matcher.group(2).equals(config.getType())) { + final String fastCompatibleString = matcher.group(1).replace('?', ' '); - if (config.getFilterField() == null) { - return fastCompatibleString; - } else { - return config.getFilterField() + ":(\"" + fastCompatibleString + "\")"; - } - } else { - return ""; + + return null == config.getFilterField() + ? fastCompatibleString + : config.getFilterField() + ":(\"" + fastCompatibleString + "\")"; } } } - return ""; + return newQuery.toString(); } } Property changes on: trunk/query-transform-control-spi/src/main/java/no/schibstedsok/searchportal/query/transform/NewsMyNewsQueryTransformer.java ___________________________________________________________________ Name: svn:keywords + Id _______________________________________________ Kernel-commits mailing list [email protected] http://sesat.no/mailman/listinfo/kernel-commits
