--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.dao.impl;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import com.pentila.evalcomp.domain.User;
+import com.pentila.evalcomp.synchronization.model.Attribute;
+import com.pentila.evalcomp.synchronization.model.Comparator;
+import com.pentila.evalcomp.synchronization.model.Filter;
+import com.pentila.evalcomp.synchronization.model.IComponent;
+import com.pentila.evalcomp.synchronization.model.Identifier;
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class DaoFilters.
+ */
+public class DaoFilters {
+
+ /**
+ * Builds the sql filter.
+ *
+ * @param dbmap
+ * the dbmap
+ * @param components
+ * the components
+ * @param not
+ * the not
+ * @param table
+ * the table
+ *
+ * @return the string
+ */
+ public static String buildSQLFilter(List<Map<String, String>> dbmap,
+ Set<IComponent> components, boolean not, String table) {
+ Set<String> idents = new HashSet<String>();
+ Set<String> comps = new HashSet<String>();
+ Set<String> attrs = new HashSet<String>();
+
+ // Prepare strucutres form filtering attrs
+ for (IComponent ic : components) {
+
+ String db = ic.getDb();
+
+ if (ic instanceof Identifier) {
+ idents.add(db);
+ } else if (ic instanceof Comparator) {
+ comps.add(db);
+ } else if (ic instanceof Attribute) {
+ attrs.add(db);
+ }
+
+ }
+
+ String filter = "";
+
+ if (!table.equals("") && !dbmap.isEmpty()) {
+
+ String namedItem = table + "Item";
+
+ filter = String.format("SELECT %s FROM %s as %s WHERE", namedItem,
+ table, namedItem);
+
+ Iterator<Map<String, String>> i = dbmap.iterator();
+ boolean firstTime = true;
+ while (i.hasNext()) {
+
+ Map<String, String> map = i.next();
+
+ Iterator<Entry<String, String>> j = map.entrySet().iterator();
+
+ String filterIdent = "";
+ String filterComp = "";
+
+ while (j.hasNext()) {
+ Entry<String, String> e = j.next();
+ String key = e.getKey();
+
+ if (idents.contains(key) || comps.contains(key)) {
+
+ String value = e.getValue();
+ String op = "=";
+
+ if (comps.contains(key)) {
+ // this is a comparator
+ if (not) {
+ // take the opposite
+ op = "!" + op;
+ }
+ String oplog = "OR";
+ if (filterComp.equals("")) {
+ oplog = "";
+ }
+
+ filterComp = String.format("%s %s %s.%s%s'%s'",
+ filterComp, oplog, namedItem, key, op,
+ value);
+
+ } else {
+
+ String oplog = "AND";
+ if (filterIdent.equals("")) {
+ oplog = "";
+ }
+
+ filterIdent = String.format("%s %s %s.%s%s'%s'",
+ filterIdent, oplog, namedItem, key, op,
+ value);
+ }
+ }
+ }
+
+ String ff = "";
+ if (filterComp.equals("")) {
+ ff = String.format("(%s)", filterIdent);
+ } else {
+ ff = String.format("((%s) AND (%s))", filterIdent,
+ filterComp);
+ }
+
+ if (firstTime) {
+ filter = String.format("%s %s", filter, ff);
+ firstTime = false;
+ } else {
+ filter = String.format("%s OR %s", filter, ff);
+ }
+
+ }
+ }
+ return filter;
+ }
+
+ /**
+ * Builds a db string filter with the comparators list and the given table
+ * name. The filter will be like: SELECT table FROM tables as table WHERE
+ * table.id IN (SELECT table.id FROM table as id0 WHERE table.table IN
+ * (:p0)) and as many imbricated select's as the comparators list size is.
+ *
+ * @param comparators
+ * the list of comparators
+ * @param table
+ * the table name E.g. table="User", "Group" -> hibernate mapping
+ * rules
+ *
+ * @return the builded string filter
+ */
+ public static String buildSQLFilterWithComparators(
+ List<Identifier> comparators, String table) {
+ String filter = String.format("SELECT %s FROM %s as %s ", table, table,
+ table);
+
+ if (comparators.size() >= 1) {
+ filter = String
+ .format(" %s WHERE %s.id IN (SELECT %s.id FROM %s as id0 WHERE %s.%s IN (:p0)) ",
+ filter, table, table, table, table, comparators
+ .get(0).getDb());
+
+ if (comparators.size() > 1) {
+ for (int i = 1; i < comparators.size(); i++) {
+ filter = String
+ .format(" %s AND %s.id IN (SELECT %s.id FROM %s as id%d WHERE %s.%s IN (:p%d)) ",
+ filter, table, table, table, i, table,
+ comparators.get(i).getDb(), i);
+ }
+ }
+ }
+ return filter;
+ }
+
+ /**
+ * Builds a db string filter with the filters list and the given table name.
+ * A filter will contain one valid db " Where condition " . The filter will
+ * be like: SELECT table FROM tables as table WHERE table.filter and as many
+ * conditions as the filters list size is. The reference user list will
+ * force to apply the filters only to this list. So the filter will search
+ * in db for this list of users matching the given filters.
+ *
+ * @param filters
+ * the list of filters
+ * @param referenceList
+ * is the list of users that will be filtered with the filters
+ * @param table
+ * the table name E.g. table="User", "Group" -> hibernate mapping
+ * rules
+ *
+ * @return the builded string filter
+ */
+ public static String buildSQLFilterWithFilters(Set<Filter> filters,
+ Set<User> referenceList, String table) {
+ String filter = String.format("SELECT %s FROM %s as %s ", table, table,
+ table);
+
+ Iterator<Filter> it = filters.iterator();
+
+ if (filters.size() >= 1) {
+ filter = String.format(" %s WHERE %s.%s ", filter, table, it.next()
+ .getValue());
+
+ while (it.hasNext()) {
+ filter = String.format(" %s AND %s.%s ", filter, table, it
+ .next().getValue());
+ }
+ }
+ return filter;
+ }
+
+}