Author: fdesbois Date: 2009-12-29 12:17:31 +0000 (Tue, 29 Dec 2009) New Revision: 161 Removed: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/TopiaQuery.java Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/QueryBuilder.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatImpl.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatInfosImpl.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/ProgramImpl.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/SampleRowImpl.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ActivityCalendarImport.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceBoatImpl.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceContactImpl.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceSamplingImpl.java trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceUserImpl.java trunk/suiviobsmer-ui/src/main/resources/log4j.properties Log: - Move TopiaQuery to ToPIA - Improve activityCalendar import and boat import Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/QueryBuilder.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/QueryBuilder.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/QueryBuilder.java 2009-12-29 12:17:31 UTC (rev 161) @@ -21,12 +21,18 @@ package fr.ifremer.suiviobsmer; -import fr.ifremer.suiviobsmer.TopiaQuery.Op; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery.Op; import fr.ifremer.suiviobsmer.bean.ContactState; import fr.ifremer.suiviobsmer.entity.Boat; +import fr.ifremer.suiviobsmer.entity.BoatImpl; import fr.ifremer.suiviobsmer.entity.Company; import fr.ifremer.suiviobsmer.entity.Contact; import java.util.Date; +import org.hibernate.SQLQuery; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaQuery; /** * QueryBuilder @@ -88,4 +94,12 @@ return query; } +// public static Boat getBoatByImmatriculation(TopiaContext transaction, int immatriculation) throws TopiaException { +// TopiaContextImplementor topia = (TopiaContextImplementor)transaction; +// SQLQuery query = topia.getHibernate().createSQLQuery( +// "SELECT * FROM boat WHERE immatriculation = " + immatriculation); +// +// return (Boat)query.addEntity(BoatImpl.class).uniqueResult(); +// } + } Deleted: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/TopiaQuery.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/TopiaQuery.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/TopiaQuery.java 2009-12-29 12:17:31 UTC (rev 161) @@ -1,583 +0,0 @@ -/* - * *##% - * SuiviObsmer :: Business - * Copyright (C) 2009 Ifremer - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * ##%* - */ - -package fr.ifremer.suiviobsmer; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; -import org.apache.commons.beanutils.PropertyUtils; -import org.apache.commons.lang.StringUtils; -import org.nuiton.topia.TopiaContext; -import org.nuiton.topia.TopiaException; -import org.nuiton.topia.persistence.TopiaEntity; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * TopiaQuery - * - * Created: 21 déc. 2009 - * - * @author fdesbois - * @version $Revision$ - * - * Mise a jour: $Date$ - * par : $Author$ - */ -public class TopiaQuery { - - private static final Logger log = LoggerFactory.getLogger(TopiaQuery.class); - - /** Params for HQL query **/ - protected List<Object> params; - - /** Select part of the query **/ - protected String select; - - /** From part of the query **/ - protected String from; - - /** Where part of the query **/ - protected String where; - - /** Order By part of the query **/ - protected String orderBy; - - /** Group By part of the query **/ - protected String groupBy; - - /** Used to determine if parentheses are needed for Where input **/ - protected boolean parentheses; - - protected List<String> propertiesToLoad; - - /** - * Enum to simmplify using operation in query - */ - public static enum Op { - /** EQUALS **/ - EQ("="), - /** GREATER THAN **/ - GT(">"), - /** GREATER OR EQUALS **/ - GE(">="), - /** LIKE for String manipulation **/ - LIKE("LIKE"), - /** LESS THAN **/ - LT("<"), - /** LESS OR EQUALS **/ - LE("<="), - /** IS NOT NULL **/ - NOT_NULL("IS NOT NULL"); - - protected String value; - - /** - * Constructor of the Op Enum. - * - * @param value corresponding to the String for the query - */ - Op(String value) { - this.value = value; - } - - @Override - public String toString() { - return this.value; - } - } - - /** - * Constructor of TopiaQuery with entityClass initialization. - * - * @param entityClass Class for an entity Query - */ - public TopiaQuery(Class<? extends TopiaEntity> entityClass) { - this(entityClass.getName()); - } - - /** - * Constructor of TopiaQuery with String form initialization. - * - * @param from From part for the Query - */ - public TopiaQuery(String from) { - this.from = " FROM " + from; - parentheses = true; - } - - @Override - public String toString() { - return fullQuery(); - } - - /** - * Get the full query. - * - * @return a String corresponding to the full query. - */ - public String fullQuery() { - String result = ""; - if (select != null) { - result = select; - } - result += from; - if (where != null) { - result += where; - } - if (groupBy != null) { - result += groupBy; - } - if (orderBy != null) { - result += orderBy; - } - return result.trim(); - } - - /** - * Add a HQL parameter to the Query. - * - * @param id identification of the param in the query - * @param e value of the param - * @return the TopiaQuery - */ - public TopiaQuery addParam(String id, Object e) { - if (params == null) { - params = new ArrayList<Object>(); - } - params.add(id); - params.add(e); - return this; - } - - /** - * Add a property to load when query is executed. - * Used to avoid LazyInitializationException for property needed after closing context. - * The property is a string like those in HQL query. - * <pre> - * Exemples : - * - "person.company" (Property TopiaEntity person linked to the result entity in query and company linked to person) - * --> calling myEntity.getPerson().getCompany(); - * - "partyRoles" (Property Collection partyRoles linked to the result entity in query) - * --> calling myEntity.getPartyRoles().size(); - * </pre> - * - * @param properties - * @return - */ - public TopiaQuery addLoad(String... properties) { - getPropertiesToLoad().addAll(Arrays.asList(properties)); - return this; - } - - protected List<String> getPropertiesToLoad() { - if (propertiesToLoad == null) { - propertiesToLoad = new ArrayList<String>(); - } - return propertiesToLoad; - } - - /** - * Add a where element to the Query. Could be anything. - * Parentheses are added automatically (even if there are not needed). - * - * @param where element to add - * @return the TopiaQuery - */ - public TopiaQuery add(String where) { - if (this.where == null) { - this.where = " WHERE "; - } else { - this.where += " AND "; - } - if (parentheses) { - this.where += "("; - } - this.where += where; - if (parentheses) { - this.where += ")"; - } - parentheses = true; - return this; - } - - /** - * Add an element to the query. The parameter will be automatically added. - * The constraint is needed to determine what type of operation it is. - * - * @param paramName the name of the parameter in the query (attribute of the entity) - * @param constraint the operation concerned - * @param paramValue the value of the parameter (an other entity, a String, ...) - * @return the TopiaQuery - */ - public TopiaQuery add(String paramName, Op constraint, Object paramValue) { - int dot = paramName.lastIndexOf("."); - String valueName = paramName; - if (dot != -1) { - valueName = paramName.substring(dot+1); - } - parentheses = false; - return add(paramName + " " + constraint + " :" + valueName).addParam(valueName, paramValue); - } - - /** - * Add an element to the query with the constraint Not null. - * - * @param paramName name of the parameter in the query - * @return the TopiaQuery - */ - public TopiaQuery addNotNull(String paramName) { - return add(paramName + " " + Op.NOT_NULL); - } - - /** - * Add an element to the query. The parameter will be automatically added. - * The default constrainst operation is Op.EQ for EQUALS. - * Ex : add("boat", boat) means -> boat = :boat. - * - * @param paramName name of the parameter in the query - * @param paramValue value of the parameter - * @return the TopiaQuery - * @see #add(java.lang.String, fr.ifremer.suiviobsmer.TopiaQuery.Op, java.lang.Object) - */ - public TopiaQuery add(String paramName, Object paramValue) { - return add(paramName, Op.EQ, paramValue); - } - - /** - * Add an element to the from in the query. Used to add some other data in the query or for join. - * - * @param str the element to add - * @return the TopiaQuery - */ - public TopiaQuery addFrom(String str) { - from += ", " + str; - return this; - } - - /** - * Add an element to the select in the query. Used to add some parameters for the return of query. - * - * @param select element to add - * @return the TopiaQuery - */ - public TopiaQuery addSelect(String select) { - if (this.select == null) { - this.select = "SELECT "; - } else { - this.select += ", "; - } - this.select += select; - return this; - } - - /** - * Add an element to the order in the query. Used to add some parameters to order by. - * - * @param order element to add - * @return the TopiaQuery - */ - public TopiaQuery addOrder(String order) { - if (orderBy == null) { - orderBy = " ORDER BY "; - } else { - orderBy += ", "; - } - orderBy += order; - return this; - } - - public TopiaQuery addOrderDesc(String order) { - return addOrder(order + " DESC"); - } - - /** - * Add an element to the group of the query. Used to add some paramters to group by. - * - * @param group element to add - * @return the TopiaQuery - */ - public TopiaQuery addGroup(String group) { - if (groupBy == null) { - groupBy = " GROUP BY "; - } else { - groupBy += ", "; - } - groupBy += group; - return this; - } - - /** - * Simple execution of the query. This method use directly the find method in TopiaContext interface. - * - * @param transaction the TopiaContext to use for execution - * @return a List of results - * @throws TopiaException - * @see org.nuiton.topia.TopiaContext#find(java.lang.String, java.lang.Object[]) - */ - public List execute(TopiaContext transaction) throws TopiaException { - if (log.isTraceEnabled()) { - log.trace("# QUERY : " + fullQuery()); - if (params != null) { - log.trace("# PARAMS : " + Arrays.toString(params.toArray())); - } - } - if (params == null) { - return transaction.find(fullQuery()); - } - return transaction.find(fullQuery(), params.toArray()); - } - - /** - * Execute the query and get a List of entity. Some properties will be loaded if they are - * prealably set using ${@link #addLoad(java.lang.String[]) }. - * - * @param <T> the type of TopiaEntity to return - * @param transaction the TopiaContext to use for execution - * @param entityClass the class of the TopiaEntity used for return type - * @return a List of TopiaEntity corresponding to the entityClass in argument - * @throws TopiaException - * @throws ClassCastException - */ - public <T extends TopiaEntity> List<T> executeToEntityList(TopiaContext transaction, Class<T> entityClass) - throws TopiaException, ClassCastException { - List res = execute(transaction); - if (log.isTraceEnabled()) { - log.trace("Properties to load : " + getPropertiesToLoad()); - } - List<T> results = new ArrayList<T>(); - for (Object o : res) { - if (o != null && !entityClass.isAssignableFrom(o.getClass())) { - throw new ClassCastException("Invalid cast for " + entityClass.getName()); - } - T entity = (T)o; - if (!getPropertiesToLoad().isEmpty()) { - loadProperties(entity); - } - results.add(entity); - } - return results; - } - - /** - * Load all properties for the entity. - * - * @param <T> type of the entity extends TopiaEntity - * @param entity used to load properties - * @throws TopiaException - */ - protected <T extends TopiaEntity> void loadProperties(T entity) throws TopiaException { - for (String prop : getPropertiesToLoad()) { - if (log.isTraceEnabled()) { - log.trace("load property " + prop + " ..."); - } - List<String> str = Arrays.asList(prop.split("\\.")); - Iterator<String> it = str.iterator(); - TopiaEntity currEntity = entity; - while (it.hasNext()) { - String s = it.next(); - if (log.isTraceEnabled()) { - log.trace("Current entity : " + currEntity.getClass().getSimpleName()); - log.trace("Current loading : " + s); - } - if (it.hasNext()) { - currEntity = loadEntityProperty(currEntity, s); - } else { - loadProperty(currEntity, s); - } - } - } - } - - /** - * Load a property of type TopiaEntity from an other entity. - * - * @param <T> type of the entity extends TopiaEntity - * @param entity used to load the property - * @param property name of the property in the entity - * @return a TopiaEntity corresponding to the property loaded - * @throws TopiaException - */ - protected <T extends TopiaEntity> TopiaEntity loadEntityProperty(T entity, String property) throws TopiaException { - return (TopiaEntity)loadProperty(entity, property); - } - - /** - * Load a property from an entity. - * - * @param <T> type of the entity extends TopiaEntity - * @param entity used to load the property - * @param property name of the property in the entity - * @return an Object corresponding to the property loaded - * @throws TopiaException - */ - protected <T extends TopiaEntity> Object loadProperty(T entity, String property) throws TopiaException { - try { - Object res = PropertyUtils.getProperty(entity, property); - if (log.isTraceEnabled()) { - log.trace("load property '" + property + "' for '" + entity.getClass().getSimpleName() + "'"); - } - if (Collection.class.isAssignableFrom(res.getClass())) { - Collection list = (Collection) res; - list.size(); - } - return res; - } catch (IllegalAccessException eee) { - throw new TopiaException("Illegal access on property " + property + " from entity " + entity.getClass().getName(), eee); - } catch (InvocationTargetException eee) { - throw new TopiaException("Invocation error on entity " + entity.getClass().getName() + " for property " + property, eee); - } catch (NoSuchMethodException eee) { - throw new TopiaException("Getter method does not exist for property " + property + " from entity " + entity.getClass().getName(), eee); - } - } - - /** - * Execute the query and get a Map of entity with key type in argument. Some properties will be loaded if they are - * prealably set using ${@link #addLoad(java.lang.String[]) }. - * - * @param <K> the type of the map key - * @param <T> the type of entity, value of the map - * @param transaction the TopiaContext to use for execution - * @param entityClass the class of the TopiaEntity used for return - * @param keyName the property name of the key in the entity - * @param keyClass the key class for the result map - * @return a Map with the key type defined and the entity in value - * @throws TopiaException - * @throws ClassCastException - */ - public <K, T extends TopiaEntity> Map<K, T> executeToEntityMap(TopiaContext transaction, Class<T> entityClass, String keyName, Class<K> keyClass) - throws TopiaException, ClassCastException { - - Map<K, T> results = new HashMap<K, T>(); - //for (PropertyDescriptor) - for (T elmt : executeToEntityList(transaction, entityClass)) { - Object value = loadProperty(elmt, keyName); - if (value != null && !keyClass.isAssignableFrom(value.getClass())) { - throw new ClassCastException("Invalid cast for " + keyClass.getName()); - } - results.put((K)value, elmt); - } - return results; - } - - /** - * Execute the query and get a Map of entity with topiaId in key. Some properties will be loaded if they are - * prealably set using ${@link #addLoad(java.lang.String[]) }. - * - * @param <T> the type of entity, value of the map - * @param transaction the TopiaContext to use for execution - * @param entityClass the class of the TopiaEntity used for return - * @return a Map with the key type defined and the entity in value - * @throws TopiaException - * @throws ClassCastException - */ - public <T extends TopiaEntity> Map<String, T> executeToEntityMap(TopiaContext transaction, Class<T> entityClass) - throws TopiaException, ClassCastException { - return executeToEntityMap(transaction, entityClass, TopiaEntity.TOPIA_ID, String.class); - } - - /** - * Execute the query and get the first result entity. Some properties will be loaded if they are - * prealably set using ${@link #addLoad(java.lang.String[]) }. - * - * @param <T> the type of TopiaEntity to return - * @param transaction the TopiaContext to use for execution - * @param entityClass the class of the TopiaEntity used for return type - * @return a TopiaEntity corresponding to the entityClass in argument - * @throws TopiaException - * @throws ClassCastException - */ - public <T extends TopiaEntity> T executeToEntity(TopiaContext transaction, Class<T> entityClass) - throws TopiaException, ClassCastException { - List<T> results = executeToEntityList(transaction, entityClass); - return !results.isEmpty() ? results.get(0) : null; - } - - /** - * Execute the query and get an Integer for result. Used for query with COUNT or SUM, ... - * The select is overriden to get only the right value for return. - * - * @param transaction the TopiaContext to use for execution - * @param select the Select overriden (ex : SUM(myParam)) - * @return an Integer - * @throws TopiaException - */ - public int executeToInteger(TopiaContext transaction, String select) throws TopiaException { - Long res = (Long)executeToObject(transaction, select); - return res != null ? res.intValue() : 0; - } - - /** - * Execute the query and get a String for result. Used for query with MAX, ... - * The select is overriden to get only the right value for return. - * - * @param transaction the TopiaContext to use for execution - * @param select the Select overriden (ex : MAX(myParam)) - * @return a String - * @throws TopiaException - */ - public String executeToString(TopiaContext transaction, String select) throws TopiaException { - Object res = executeToObject(transaction, select); - return res != null ? (String)res : ""; - } - - /** - * Execute the query and get an Object for result. - * The select is overriden to get only the right value for return. - * - * @param transaction the TopiaContext to use for execution - * @param select the Select overriden - * @return an Object - * @throws TopiaException - */ - public Object executeToObject(TopiaContext transaction, String select) throws TopiaException { - String oldValue = this.select; - if (!StringUtils.isEmpty(select)) { - this.select = "SELECT " + select; - } - Object result = null; - List results = execute(transaction); - if (!results.isEmpty()) { - result = results.get(0); - } - this.select = oldValue; - return result; - } - - /** - * Execute a simple count on the query, i.e. the number of results get from the query. - * - * @param transaction the TopiaContext to use for execution - * @return an int corresponding to the number of result in the query - * @throws TopiaException - */ - public int executeCount(TopiaContext transaction) throws TopiaException { - return executeToInteger(transaction, "COUNT(*)"); - } - -} Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -24,7 +24,7 @@ import fr.ifremer.suiviobsmer.QueryBuilder; import fr.ifremer.suiviobsmer.SuiviObsmerException; import fr.ifremer.suiviobsmer.SuiviObsmerContext; -import fr.ifremer.suiviobsmer.TopiaQuery; +import org.nuiton.topia.framework.TopiaQuery; import fr.ifremer.suiviobsmer.bean.ContactState; import java.io.Serializable; import org.nuiton.topia.TopiaContext; @@ -63,18 +63,6 @@ try { transaction = SuiviObsmerContext.getTopiaRootContext().beginTransaction(); -// String contactClassName = Contact.class.getName(); -// // Return the last contact for this boat and company -// List results = transaction.find( -// "FROM " + contactClassName + " C1" + -// " WHERE C1.user.company = :company" + -// " AND C1.boat = :boat" + -// " AND C1.topiaCreateDate = " + -// " (SELECT MAX(C2.topiaCreateDate) FROM " + contactClassName + " C2" + -// " WHERE C2.user.company = :company AND C2.boat = C1.boat)", -// "company", company, -// "boat", this); - TopiaQuery query = QueryBuilder.createQueryLastContactForBoat(this, company); Contact contact = query.executeToEntity(transaction, Contact.class); if (contact != null) { Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatInfosImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatInfosImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/BoatInfosImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -24,7 +24,7 @@ import fr.ifremer.suiviobsmer.QueryBuilder; import fr.ifremer.suiviobsmer.SuiviObsmerContext; import fr.ifremer.suiviobsmer.SuiviObsmerException; -import fr.ifremer.suiviobsmer.TopiaQuery; +import org.nuiton.topia.framework.TopiaQuery; import java.io.Serializable; import org.nuiton.topia.TopiaContext; import java.util.Date; @@ -109,53 +109,4 @@ } return result; } - - /** - * Execute the HQL query for counting contact with BoardingDone state since the fromDate. - * This query is used to get all boarding dones on this boat for all companies or only for the - * current one. Only the validate contacts by company will be count. The param fromDate can be null, - * in this case, all contacts will be counted (not matter which date then started). - * - * @param transaction TopiaContext to execute the query - * @param fromDate the date to start calcul - * @param companyOnly boolean used to filter by the current company or not - * @return the number of boarding done since fromDate - * @throws TopiaException - */ -// protected int countDoneContactsFromDate(TopiaContext transaction, Date fromDate, boolean companyOnly) throws TopiaException { -// int result = 0; -// -// List<Object> params = new ArrayList<Object>(); -// String contactClassName = Contact.class.getName(); -// String query = "SELECT COUNT(*)" + -// " FROM " + contactClassName + -// " WHERE boat = :boat AND state = :boardingDone" + -// " AND validationCompany = :booleanTrue" + -// " AND (validationProgram IS NULL OR validationProgram = :booleanTrue)"; -// -// params.add("boat"); params.add(getBoat()); -// params.add("boardingDone"); params.add(ContactState.BOARDING_DONE.toString()); -// params.add("booleanTrue"); params.add(Boolean.TRUE); -// -// if (fromDate != null) { -// query += " AND tideBeginDate >= :fromDate"; -// params.add("fromDate"); -// params.add(fromDate); -// } -// -// if (companyOnly) { -// query+= " AND user.company = :company"; -// params.add("company"); -// params.add(getCompany()); -// } -// -// List results = transaction.find(query, params.toArray()); -// -// if (!results.isEmpty()) { -// Long tmp = (Long) results.get(0); -// result += tmp.intValue(); -// } -// -// return result; -// } } Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/ProgramImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/ProgramImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/ProgramImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -23,8 +23,8 @@ import fr.ifremer.suiviobsmer.SuiviObsmerContext; import fr.ifremer.suiviobsmer.SuiviObsmerException; -import fr.ifremer.suiviobsmer.TopiaQuery; -import fr.ifremer.suiviobsmer.TopiaQuery.Op; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.framework.TopiaQuery.Op; import java.io.Serializable; import java.text.NumberFormat; import java.util.Calendar; @@ -91,19 +91,11 @@ Calendar refBegin = new GregorianCalendar(); refBegin.setTime(begin); refBegin.set(Calendar.DAY_OF_MONTH, 1); - /*refBegin.set(Calendar.HOUR, 0); - refBegin.set(Calendar.MINUTE, 0); - refBegin.set(Calendar.SECOND, 0); - refBegin.set(Calendar.MILLISECOND, 0);*/ Calendar refEnd = new GregorianCalendar(); refEnd.setTime(end); int max = refEnd.getActualMaximum(Calendar.DAY_OF_MONTH); refEnd.set(Calendar.DAY_OF_MONTH, max); - /*refBegin.set(Calendar.HOUR, 0); - refBegin.set(Calendar.MINUTE, 0); - refBegin.set(Calendar.SECOND, 0); - refBegin.set(Calendar.MILLISECOND, 0);*/ return ! (refEnd.before(getCalendarBegin()) || refBegin.after(getCalendarEnd())); } Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/SampleRowImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/SampleRowImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/entity/SampleRowImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -201,11 +201,4 @@ month.addRealTideTime(-nbDays); } -// protected int getDifferenceDays(Contact contact) { -// int nbDays = DateUtils.getDifferenceInDays(contact.getTideBeginDate(), contact.getTideEndDate()); -// // difference nbDays + 1 for the first day -// nbDays++; -// return nbDays; -// } - } Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ActivityCalendarImport.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ActivityCalendarImport.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ActivityCalendarImport.java 2009-12-29 12:17:31 UTC (rev 161) @@ -25,6 +25,7 @@ import com.csvreader.CsvReader; import fr.ifremer.suiviobsmer.entity.ActivityCalendar; import fr.ifremer.suiviobsmer.entity.ActivityCalendarDAO; +import fr.ifremer.suiviobsmer.entity.ActivityCalendarImpl; import fr.ifremer.suiviobsmer.entity.ActivityMonth; import fr.ifremer.suiviobsmer.entity.ActivityMonthDAO; import fr.ifremer.suiviobsmer.entity.ActivityProfession; @@ -32,7 +33,6 @@ import fr.ifremer.suiviobsmer.entity.ActivityZone; import fr.ifremer.suiviobsmer.entity.ActivityZoneDAO; import fr.ifremer.suiviobsmer.entity.Boat; -import fr.ifremer.suiviobsmer.entity.BoatDAO; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; @@ -43,11 +43,16 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Arrays; import java.util.Date; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.time.DurationFormatUtils; +import org.hibernate.SQLQuery; import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.framework.TopiaContextImplementor; +import org.nuiton.topia.framework.TopiaQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,6 +75,54 @@ private static final Logger log = LoggerFactory.getLogger(ActivityCalendarImport.class); + private static final boolean DISABLED = false; + + // CREATE UNIQUE INDEX boat_immatriculation_idx ON BOAT(IMMATRICULATION) + // CREATE UNIQUE INDEX activitycalendar_idx ON ACTIVITYCALENDAR (BOAT, YEAR) + // CREATE UNIQUE INDEX shipowner_idx ON SHIPOWNER (CODE) + + public class ActivityCalendarKey { + + private Boat _boat; + private String _boatId; + private int _year; + + public ActivityCalendarKey(Boat boat, int year) { + _boatId = boat.getTopiaId(); + _year = year; + _boat = boat; + } + + public String boatId() { + return _boatId; + } + + public Boat boat() { + return _boat; + } + + public int year() { + return _year; + } + + @Override + public boolean equals(Object key) { + if (!ActivityCalendarKey.class.isAssignableFrom(key.getClass())) { + throw new ClassCastException("Unable to cast object " + key.getClass() + " to " + this.getClass().getName()); + } + ActivityCalendarKey calendarKey = (ActivityCalendarKey)key; + return _year == calendarKey.year() && _boatId.equals(calendarKey.boatId()); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 61 * hash + (this._boatId != null ? this._boatId.hashCode() : 0); + hash = 61 * hash + this._year; + return hash; + } + } + public ActivityCalendarImport(InputStream input) throws IOException { super(); this.input = input; @@ -100,67 +153,105 @@ int result = 0; + long firstTic = startTime; + + long tic1, tic2; + + Map<ActivityCalendarKey, ActivityCalendar> availableCalendars = new HashMap<ActivityCalendarKey, ActivityCalendar>(); + + //BoatDAO boatDAO = SuiviObsmerModelDAOHelper.getBoatDAO(transaction); + ActivityCalendarDAO calendarDAO = SuiviObsmerModelDAOHelper.getActivityCalendarDAO(transaction); + ActivityMonthDAO monthDAO = SuiviObsmerModelDAOHelper.getActivityMonthDAO(transaction); + ActivityProfessionDAO professionDAO = SuiviObsmerModelDAOHelper.getActivityProfessionDAO(transaction); + ActivityZoneDAO zoneDAO = SuiviObsmerModelDAOHelper.getActivityZoneDAO(transaction); + while(reader.readRecord()) { currRow++; - String timeLog = ""; - long firstTic = System.currentTimeMillis(); - //result[0]++; - -// if (log.isTraceEnabled()) { -// log.trace(" " + Arrays.asList(reader.getValues())); -// } + //long firstTic = System.currentTimeMillis(); int boatImmatriculation = Integer.parseInt(reader.get("NAVS_COD").trim()); - BoatDAO boatDAO = SuiviObsmerModelDAOHelper.getBoatDAO(transaction); +// long tic1 = System.currentTimeMillis(); - Boat boat = boatDAO.findByImmatriculation(boatImmatriculation); + //Boat boat = boatDAO.findByImmatriculation(boatImmatriculation); +// TopiaContextImplementor topia = (TopiaContextImplementor)transaction; +// SQLQuery query = topia.getHibernate().createSQLQuery( +// "SELECT * FROM boat WHERE immatriculation = " + boatImmatriculation); +// +// Boat boat = (Boat)query.addEntity(BoatImpl.class).uniqueResult(); + Boat boat = new TopiaQuery(Boat.class).add(Boat.IMMATRICULATION, boatImmatriculation).executeToEntity(transaction); + +// long tic2 = System.currentTimeMillis(); +// log.info("findByImmatriculation : " + (tic2 - tic1)); if (boat != null) { + + int year = Integer.parseInt(reader.get("SYNA_AN").trim()); - ActivityCalendarDAO dao = SuiviObsmerModelDAOHelper.getActivityCalendarDAO(transaction); + ActivityCalendarKey key = new ActivityCalendarKey(boat, year); + ActivityCalendar calendar = availableCalendars.get(key); - int year = Integer.parseInt(reader.get("SYNA_AN").trim()); +// tic1 = System.currentTimeMillis(); - ActivityCalendar calendar = dao.findByProperties( - ActivityCalendar.BOAT, boat, - ActivityCalendar.YEAR, year); - + // If not available select it from Database if (calendar == null) { - int fiability = Integer.parseInt(reader.get("INDQ_COD").trim()); +// TopiaContextImplementor topia = (TopiaContextImplementor)transaction; +// SQLQuery query = topia.getHibernate().createSQLQuery( +// "SELECT * FROM activityCalendar WHERE boat = '" + key.boatId() + "' AND year = " + key.year()); +// +// calendar = (ActivityCalendar)query.addEntity(ActivityCalendarImpl.class).uniqueResult(); - calendar = dao.create( - ActivityCalendar.BOAT, boat, - ActivityCalendar.YEAR, year, - ActivityCalendar.FIABILITY, fiability); + calendar = new TopiaQuery(ActivityCalendar.class). + add(ActivityCalendar.BOAT, boat). + add(ActivityCalendar.YEAR, year). + executeToEntity(transaction); - calendar.setActivityMonth(new ArrayList<ActivityMonth>()); + // Create it if not exist in Database + if (calendar == null) { + int fiability = Integer.parseInt(reader.get("INDQ_COD").trim()); - if (log.isDebugEnabled()) { - info("Création d'un calendrier " + year + - " pour le navire immatriculé " + boatImmatriculation, currRow); + calendar = calendarDAO.create( + ActivityCalendar.BOAT, key.boat(), + ActivityCalendar.YEAR, key.year(), + ActivityCalendar.FIABILITY, fiability); + + calendar.setActivityMonth(new ArrayList<ActivityMonth>()); + + if (log.isDebugEnabled()) { + info("Création d'un calendrier " + year + + " pour le navire immatriculé " + boatImmatriculation, currRow); + } } + availableCalendars.put(key, calendar); } + +// ActivityCalendar calendar = calendarDAO.findByProperties( +// ActivityCalendar.BOAT, boat, +// ActivityCalendar.YEAR, year); + +// tic2 = System.currentTimeMillis(); +// log.info("calendar.findByProperties : " + (tic2 - tic1)); + + String calendarCode = "[ calendrier " + year + ", navire " + boatImmatriculation + " ]"; int monthNum = Integer.parseInt(reader.get("SYNA_MOI").trim()); String harbourCode = reader.get("SYNA_POR_COD").trim(); boolean active = !harbourCode.equals("INA"); + ActivityMonth month = calendar.getActivityMonth(monthNum); if (month == null) { - - ActivityMonthDAO monthDAO = SuiviObsmerModelDAOHelper.getActivityMonthDAO(transaction); month = monthDAO.create( ActivityMonth.ACTIVITY_CALENDAR, calendar, ActivityMonth.MONTH, monthNum); month.setActivityProfession(new ArrayList<ActivityProfession>()); - if (log.isDebugEnabled()) { - info("Création du mois " + monthNum + " actif(" + active + ") " + calendarCode, currRow); - } +// if (log.isDebugEnabled()) { +// info("Création du mois " + monthNum + " actif(" + active + ") " + calendarCode, currRow); +// } calendar.addActivityMonth(month); } @@ -180,9 +271,11 @@ Integer nbBoardingPersons = !nbBoardingPersonsString.equals("NA") ? Integer.valueOf(nbBoardingPersonsString) : null; - int harbourId = Integer.parseInt(reader.get("SYNA_TPOR_COD").trim()); + String harbourIdString = reader.get("SYNA_TPOR_COD").trim(); + int harbourId = !harbourIdString.equals("NA") ? Integer.parseInt(harbourIdString) : -1; String harbourLibelle = reader.get("SYNA_POR_LIB").trim(); - //String harbourCode = reader.get("SYNA_POR_COD").trim(); + harbourLibelle = !harbourLibelle.equals("NA") ? harbourLibelle : null; + harbourCode = !harbourCode.equals("NA") ? harbourCode : null; month.setHarbourId(harbourId); month.setHarbourCode(harbourCode); @@ -195,24 +288,21 @@ String professionCode = reader.get("MET_COD").trim(); int professionId = Integer.parseInt(reader.get("MET_ID").trim()); String professionLibelle = reader.get("MET_LIB").trim(); - ActivityProfession profession = month.getActivityProfession(professionOrder); if (profession == null) { - ActivityProfessionDAO professionDAO = - SuiviObsmerModelDAOHelper.getActivityProfessionDAO(transaction); profession = professionDAO.create( ActivityProfession.ACTIVITY_MONTH, month, ActivityProfession.PROFESSION_ORDER, professionOrder); profession.setActivityZone(new ArrayList<ActivityZone>()); - if (log.isDebugEnabled()) { - info("Création du métier " + professionOrder + " code(" + professionCode + ") " + - "[ mois " + monthNum + " ] " + - calendarCode, currRow); - } +// if (log.isDebugEnabled()) { +// info("Création du métier " + professionOrder + " code(" + professionCode + ") " + +// "[ mois " + monthNum + " ] " + +// calendarCode, currRow); +// } month.addActivityProfession(profession); } @@ -226,29 +316,26 @@ String gradiantLibelle = reader.get("GRA_LIB").trim(); int zoneId = Integer.parseInt(reader.get("TSECT_COD").trim()); String zoneLibelle = reader.get("SECT_LIB").trim(); + ActivityZone zone = profession.getActivityZone(zoneCode); if (zone == null) { - - ActivityZoneDAO zoneDAO = - SuiviObsmerModelDAOHelper.getActivityZoneDAO(transaction); - zone = zoneDAO.create( ActivityZone.ZONE_CODE, zoneCode, ActivityZone.ACTIVITY_PROFESSION, profession); - if (log.isDebugEnabled()) { - info("Création de la zone " + zoneCode + " [ mois " + monthNum + " ] " + - calendarCode, currRow); - } +// if (log.isDebugEnabled()) { +// info("Création de la zone " + zoneCode + " [ mois " + monthNum + " ] " + +// calendarCode, currRow); +// } profession.addActivityZone(zone); } else { - if (log.isDebugEnabled()) { - info("Ecrasement de la zone " + zoneCode + " [ mois " + monthNum + " ] " + - calendarCode, currRow); - } +// if (log.isDebugEnabled()) { +// info("Ecrasement de la zone " + zoneCode + " [ mois " + monthNum + " ] " + +// calendarCode, currRow); +// } //warn("Zone déjà existante [" + zoneCode + "] ! :: " + Arrays.toString(reader.getValues()), currRow); } @@ -264,18 +351,37 @@ warn("Navire inexistant dans le référentiel : " + boatImmatriculation, currRow); } - if (log.isDebugEnabled()) { - long tic = System.currentTimeMillis(); - log.debug("Time : " + DurationFormatUtils.formatDurationHMS(tic - firstTic)); - } +// if (log.isDebugEnabled()) { +// long tic = System.currentTimeMillis(); +// log.debug("Time : " + DurationFormatUtils.formatDurationHMS(tic - firstTic)); +// } - if (result%500 == 0 && log.isInfoEnabled()) { - log.info("RUNNING... Import calendrier d'activité ligne " + currRow); + if (result % 1000 == 0) { + tic1 = System.currentTimeMillis(); + + transaction.commitTransaction(); + availableCalendars.clear(); + + tic2 = System.currentTimeMillis(); + if (log.isDebugEnabled()) { + log.debug("commit : " + (tic2 - tic1)); + } + if (log.isInfoEnabled()) { + log.info("RUNNING... Import calendrier d'activité ligne " + currRow); + Runtime runtime = Runtime.getRuntime(); + long mem = runtime.totalMemory() - runtime.freeMemory(); + long memMega = mem / 1024 / 1024; + log.info("Memory : " + memMega + " Mo"); + long tic = System.currentTimeMillis(); + log.info("Time : " + DurationFormatUtils.formatDurationHMS(tic - firstTic)); + firstTic = System.currentTimeMillis(); + } } - - transaction.commitTransaction(); } + // For the last rows + transaction.commitTransaction(); + long stopTime = System.currentTimeMillis(); String execTime = DurationFormatUtils.formatDurationHMS(stopTime - startTime); @@ -314,8 +420,11 @@ } protected void log(String message, int numRow) throws IOException { + if (DISABLED) { + return; + } Date date = new Date(); - DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); + DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.FRENCH); String dateString = dateFormat.format(date); String ligne = numRow != -1 ? "Ligne (" + numRow + ") : " : ""; String msg = "[" + dateString + "] " + ligne + message; Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceBoatImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceBoatImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceBoatImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -28,8 +28,8 @@ import fr.ifremer.suiviobsmer.SuiviObsmerContext; import fr.ifremer.suiviobsmer.SuiviObsmerException; import fr.ifremer.suiviobsmer.SuiviObsmerModelDAOHelper; -import fr.ifremer.suiviobsmer.TopiaQuery; -import fr.ifremer.suiviobsmer.TopiaQuery.Op; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.framework.TopiaQuery.Op; import fr.ifremer.suiviobsmer.bean.BoatFilter; import fr.ifremer.suiviobsmer.bean.CompanyBoatInfos; import fr.ifremer.suiviobsmer.bean.CompanyBoatInfosImpl; @@ -50,6 +50,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.time.DurationFormatUtils; import org.nuiton.topia.TopiaContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,8 +72,6 @@ protected TopiaContext rootContext; - // CREATE UNIQUE INDEX boat_immatriculation_idx ON BOAT(IMMATRICULATION) - public ServiceBoatImpl() throws SuiviObsmerException { rootContext = SuiviObsmerContext.getTopiaRootContext(); } @@ -344,13 +343,23 @@ CsvReader reader = new CsvReader(input, Charset.forName("UTF-8")); reader.readHeaders(); + long startTime = System.currentTimeMillis(); + long tic1, tic2; + + BoatDAO boatDAO = SuiviObsmerModelDAOHelper.getBoatDAO(transaction); + ShipOwnerDAO shipOwnerDAO = SuiviObsmerModelDAOHelper.getShipOwnerDAO(transaction); + + Map<String, ShipOwner> availableShipOwners = new HashMap<String, ShipOwner>(); + + tic1 = System.currentTimeMillis(); while(reader.readRecord()) { result[0]++; - if (log.isTraceEnabled()) { - log.trace(" " + Arrays.asList(reader.getValues())); - } +// if (log.isTraceEnabled()) { +// log.trace(" " + Arrays.asList(reader.getValues())); +// } + int immatriculation = Integer.parseInt(reader.get("NAVS_COD").trim()); String boatName = reader.get("CARN_NOM").trim(); int boatLength = Integer.parseInt(reader.get("CARN_LONGUEUR_HT").trim()); @@ -362,10 +371,32 @@ String activeStr = reader.get("NAVS_ACTIVE").trim(); boolean active = !activeStr.equals("I"); - BoatDAO boatDAO = SuiviObsmerModelDAOHelper.getBoatDAO(transaction); + // Find existing boat with immatriculation - Boat boat = boatDAO.findByImmatriculation(immatriculation); + // + +// long tic1 = System.currentTimeMillis(); +// boatDAO.findByImmatriculation(immatriculation); +// long tic2 = System.currentTimeMillis(); +// if (log.isDebugEnabled()) { +// log.debug("findByImmatriculation : " + (tic2 - tic1)); +// } +// +// tic1 = System.currentTimeMillis(); +// Boat boat = QueryBuilder.getBoatByImmatriculation(transaction, immatriculation); +// tic2 = System.currentTimeMillis(); +// if (log.isDebugEnabled()) { +// log.debug("findByImmatriculation SQL : " + (tic2 - tic1)); +// } + +// tic1 = System.currentTimeMillis(); + Boat boat = new TopiaQuery(Boat.class).add(Boat.IMMATRICULATION, immatriculation).executeToEntity(transaction); +// tic2 = System.currentTimeMillis(); +// if (log.isDebugEnabled()) { +// log.debug("findByImmatriculation TopiaQuery : " + (tic2 - tic1)); +// } + if (boat == null) { boat = boatDAO.create(Boat.IMMATRICULATION, immatriculation); result[1]++; @@ -376,19 +407,56 @@ boat.setBoatLength(boatLength); boat.setActive(active); - ShipOwnerDAO shipOwnerDAO = SuiviObsmerModelDAOHelper.getShipOwnerDAO(transaction); // Find existing shipOwner with code - ShipOwner shipOwner = shipOwnerDAO.findByCode(shipOwnerCode); + //ShipOwner shipOwner = shipOwnerDAO.findByCode(shipOwnerCode); + + ShipOwner shipOwner = availableShipOwners.get(shipOwnerCode); + + // If not available, select it from database if (shipOwner == null) { - shipOwner = shipOwnerDAO.create(ShipOwner.CODE, shipOwnerCode); + shipOwner = new TopiaQuery(ShipOwner.class).add(ShipOwner.CODE, shipOwnerCode).executeToEntity(transaction); + + // If not exist yet, create it + if (shipOwner == null) { + shipOwner = shipOwnerDAO.create(ShipOwner.CODE, shipOwnerCode); + } + + availableShipOwners.put(shipOwnerCode, shipOwner); } shipOwner.setFirstName(shipOwnerFirstName); shipOwner.setLastName(shipOwnerLastName); boat.setShipOwner(shipOwner); - transaction.commitTransaction(); + //transaction.commitTransaction(); + + if (result[0] % 1000 == 0) { + availableShipOwners.clear(); + + transaction.commitTransaction(); + if (log.isInfoEnabled()) { + log.info("RUNNING... Import navires ligne " + result[0]); + Runtime runtime = Runtime.getRuntime(); + long mem = (runtime.totalMemory() - runtime.freeMemory()) / 1048576; + //long memMega = mem / 1024 / 1024; + log.info("Memory : " + mem + " Mo"); + tic2 = System.currentTimeMillis(); + log.info("Time : " + DurationFormatUtils.formatDurationHMS(tic2 - tic1)); + tic1 = System.currentTimeMillis(); + } + } } + // For the last rows + transaction.commitTransaction(); + + long stopTime = System.currentTimeMillis(); + + String execTime = DurationFormatUtils.formatDurationHMS(stopTime - startTime); + + if (log.isInfoEnabled()) { + log.info("Durée de l'import : " + execTime); + } + transaction.closeContext(); } catch (Exception eee) { SuiviObsmerContext.serviceException(transaction, Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceContactImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceContactImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceContactImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -24,7 +24,7 @@ import fr.ifremer.suiviobsmer.SuiviObsmerException; import fr.ifremer.suiviobsmer.SuiviObsmerModelDAOHelper; import fr.ifremer.suiviobsmer.SuiviObsmerContext; -import fr.ifremer.suiviobsmer.TopiaQuery; +import org.nuiton.topia.framework.TopiaQuery; import fr.ifremer.suiviobsmer.bean.states.ContactStateEnum; import fr.ifremer.suiviobsmer.entity.Boat; import fr.ifremer.suiviobsmer.entity.Company; Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceSamplingImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceSamplingImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceSamplingImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -41,12 +41,13 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; +import org.nuiton.topia.framework.TopiaQuery; +import org.nuiton.topia.framework.TopiaQuery.Op; import org.nuiton.topia.persistence.util.EntityLoador; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -233,31 +234,42 @@ List<SampleRow> results = new ArrayList<SampleRow>(); try { transaction = rootContext.beginTransaction(); - - SampleRowDAO dao = SuiviObsmerModelDAOHelper.getSampleRowDAO(transaction); + Date current = SuiviObsmerContext.getCurrentDate(); + + // Only rows which are not finished will be kept + TopiaQuery query = new TopiaQuery(SampleRow.class).add("program.periodEnd", Op.GE, current); + if (!user.getAdmin()) { - if (log.isDebugEnabled()) { - log.debug("User company : " + user.getCompany().getName()); - } - results = dao.findAllByCompany(user.getCompany()); - } else { - results = dao.findAll(); + query.add(SampleRow.COMPANY, user.getCompany()); } - Iterator<SampleRow> it = results.iterator(); - while (it.hasNext()) { - SampleRow row = it.next(); - // Only rows which are not finished will be kept - if (row.getProgram().isFinished()) { - it.remove(); - }/* else { - // load data - row.getProgram(); - row.getCompany(); - row.getProfession(); - }*/ - } + results = query.executeToEntityList(transaction, SampleRow.class); + +// SampleRowDAO dao = SuiviObsmerModelDAOHelper.getSampleRowDAO(transaction); +// +// if (!user.getAdmin()) { +// if (log.isDebugEnabled()) { +// log.debug("User company : " + user.getCompany().getName()); +// } +// results = dao.findAllByCompany(user.getCompany()); +// } else { +// results = dao.findAll(); +// } +// +// Iterator<SampleRow> it = results.iterator(); +// while (it.hasNext()) { +// SampleRow row = it.next(); +// // Only rows which are not finished will be kept +// if (row.getProgram().isFinished()) { +// it.remove(); +// }/* else { +// // load data +// row.getProgram(); +// row.getCompany(); +// row.getProfession(); +// }*/ +// } transaction.closeContext(); } catch (Exception eee) { Modified: trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceUserImpl.java =================================================================== --- trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceUserImpl.java 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-business/src/main/java/fr/ifremer/suiviobsmer/impl/ServiceUserImpl.java 2009-12-29 12:17:31 UTC (rev 161) @@ -148,13 +148,6 @@ CompanyDAO dao = SuiviObsmerModelDAOHelper.getCompanyDAO(transaction); - /*for (Company company : dao.findAll()) { - results.add(company); - // load users collection and call size() method to avoid LazyInitializationException from Hibernate - List<User> users = (List<User>)company.getUser(); - users.size(); - }*/ - if (activeOnly) { results = dao.findAllByActive(activeOnly); } else { Modified: trunk/suiviobsmer-ui/src/main/resources/log4j.properties =================================================================== --- trunk/suiviobsmer-ui/src/main/resources/log4j.properties 2009-12-24 15:57:38 UTC (rev 160) +++ trunk/suiviobsmer-ui/src/main/resources/log4j.properties 2009-12-29 12:17:31 UTC (rev 161) @@ -8,7 +8,7 @@ log4j.appender.console.Threshold=INFO log4j.appender.globalfile=org.apache.log4j.RollingFileAppender -log4j.appender.globalfile.File=${user.home}/.local/suiviobsmer/log/suiviobsmer.log +log4j.appender.globalfile.File=/var/log/suiviobsmer/suiviobsmer.log log4j.appender.globalfile.MaxFileSize=500KB log4j.appender.globalfile.Append=true log4j.appender.globalfile.MaxBackupIndex=10