/* * Created on Nov 1, 2004 * Copyright (C) Andrea Schweer, 2004 * * This file is part of the Greenstone Alerting Service. * Refer to the COPYING file in the base directory of this package * for licensing information. */ package org.greenstone.gsdlas.profiles; import java.sql.SQLException; import java.util.*; import java.util.Map; import java.util.TreeMap; import org.greenstone.gsdlas.database.DatabaseException; /** * @author schweer * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ public abstract class PredicateFactory { private static int id = 0; private static Map idEqualsPredicates = new TreeMap(); // substring for collection_name -> SubstringMatchPredicates referring to it private static Map collectionNameMatchPredicates = new TreeMap(); // query -> QueryPredicates referring to it private static Map documentTitleQueryPredicates = new TreeMap(); private static Map documentContentQueryPredicates = new TreeMap(); /** * @param key * @param value * @return * @throws ParseException * @throws DatabaseException * @throws SQLException */ public static Predicate createPredicate(String key, String value) throws SQLException, DatabaseException { System.out.println("creating predicate for key " + key + " and value " + value); if (!Predicate.isSingleValued(key)) { return null; } Predicate result = null; if (value == null || value.length() == 0) { return null; } if (Predicate.isMultiValued(key)) { List list = new ArrayList(); list.add(value); return createPredicate(key, list); } else if (key.startsWith("document")) { result = createQueryPredicate(key, value); } else if (key.endsWith("_name")) { result = createSubstringMatchPredicate(key, value); } else { return null; } result.setID(result.saveToDatabase()); return result; } public static Predicate createPredicate(String key, List values) throws SQLException, DatabaseException { if (!Predicate.isMultiValued(key)) { return null; } Predicate result = createIdEqualsPredicate(key, values); result.setID(result.saveToDatabase()); return result; } /** * @param key * @param value * @return */ private static SubstringMatchPredicate createSubstringMatchPredicate(String key, String value) { SubstringMatchPredicate predicate = null; if (key.equals("collection_name")) { if (collectionNameMatchPredicates.containsKey(value)) { predicate = (SubstringMatchPredicate) collectionNameMatchPredicates.get(value); } else { predicate = new SubstringMatchPredicate(key, value); collectionNameMatchPredicates.put(value, predicate); } } // TODO other fields than collection_name return predicate; } /** * @param key * @param values * @return */ private static IdEqualsPredicate createIdEqualsPredicate(String key, List values) { IdEqualsPredicate predicate = null; Map keyToPredicates; if (!idEqualsPredicates.containsKey(key)) { keyToPredicates = new HashMap(); idEqualsPredicates.put(key, keyToPredicates); } keyToPredicates = (Map) idEqualsPredicates.get(key); for (Iterator iter = values.iterator(); iter.hasNext();) { String value = (String) iter.next(); if (keyToPredicates.containsKey(value)) { predicate = (IdEqualsPredicate) keyToPredicates.get(value); } else { predicate = new IdEqualsPredicate(key, value); keyToPredicates.put(value, predicate); } } return predicate; } /** * @param field * @param query * @return */ private static QueryPredicate createQueryPredicate(String field, String query) { QueryPredicate predicate = new QueryPredicate(field, query); if (field.equals("document_content")) { documentContentQueryPredicates.put(query, predicate); } else if (field.equals("document_title")) { documentTitleQueryPredicates.put(query, predicate); } return predicate; } public static Collection getAllQueryPredicates() { Set result = new TreeSet(); result.addAll(documentTitleQueryPredicates.values()); result.addAll(documentContentQueryPredicates.values()); return Collections.unmodifiableSet(result); } public static Collection getQueryPredicates(String field) { Set result = new TreeSet(); if (field.equals("document_title")) { result.addAll(documentTitleQueryPredicates.values()); } else if (field.equals("document_content")) { result.addAll(documentContentQueryPredicates.values()); } return Collections.unmodifiableSet(result); } public static Collection getAllSubstringMatchPredicates() { return Collections.unmodifiableCollection(collectionNameMatchPredicates.values()); } /** * @param string * @param string2 * @return */ public static Predicate getIdEqualsPredicate(String key, String value) { return (Predicate) ((Map)idEqualsPredicates.get(key)).get(value); } }