From 4aae86335e6eed1389e2652c269d6444adcc4046 Mon Sep 17 00:00:00 2001 From: miciele Ghiorghis Date: Tue, 19 Feb 2013 15:49:20 +0100 Subject: [PATCH] tim-connector: added attribute key and connectorId and removed code and label attributes - attributes key and connectorId added - modified findBys with this new change - modified other files affected by this change --- .../orders/daos/IOrderSyncInfoDAO.java | 15 ++++-- .../orders/daos/OrderSyncInfoDAO.java | 28 +++++------ .../orders/entities/OrderSyncInfo.java | 47 +++++++++++++------ .../src/main/resources/db.changelog-1.3.xml | 13 +++-- .../business/orders/entities/Orders.hbm.xml | 6 +-- .../importers/ExportTimesheetsToTim.java | 22 +++++---- .../orders/TimSynchronizationController.java | 2 +- 7 files changed, 80 insertions(+), 53 deletions(-) diff --git a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java index 4773f179d..0bf385a6e 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/IOrderSyncInfoDAO.java @@ -34,24 +34,29 @@ public interface IOrderSyncInfoDAO extends IGenericDAO { /** * Search last synchronized info for the specified - * {@link Order} + * {@link Order} and connectorId * * @param order * the order to search for + * @param connectorId + * the connector-id * * @return Last synchronized info */ - OrderSyncInfo findByOrderLastSynchronizedInfo(Order order); + OrderSyncInfo findLastSynchronizedInfoByOrderAndConnectorId(Order order, + String connectorId); /** * Search last synchronized infos for the specified - * {@link Order} + * {@link Order} and connectorId * * @param order * the order to search for - * + * @param connectorId + * the connector-id * @return list of last synchronized infos */ - List findByOrderLastSynchronizedInfos(Order order); + List findLastSynchronizedInfosByOrderAndConnectorId( + Order order, String connectorId); } diff --git a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java index 7e19cc77d..6b0838328 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java +++ b/libreplan-business/src/main/java/org/libreplan/business/orders/daos/OrderSyncInfoDAO.java @@ -22,9 +22,6 @@ package org.libreplan.business.orders.daos; import java.util.List; import org.hibernate.Criteria; -import org.hibernate.criterion.DetachedCriteria; -import org.hibernate.criterion.Projections; -import org.hibernate.criterion.Property; import org.hibernate.criterion.Restrictions; import org.libreplan.business.common.daos.GenericDAOHibernate; import org.libreplan.business.orders.entities.Order; @@ -44,26 +41,23 @@ public class OrderSyncInfoDAO extends GenericDAOHibernate implements IOrderSyncInfoDAO { @Override - public OrderSyncInfo findByOrderLastSynchronizedInfo(Order order) { - DetachedCriteria mostRecentDate = DetachedCriteria - .forClass(OrderSyncInfo.class) - .setProjection(Projections.max("lastSyncDate")) - .add(Restrictions.isNotNull("code")); - - Criteria criteria = getSession().createCriteria(OrderSyncInfo.class); - - criteria.add(Restrictions.eq("order", order)); - criteria.add(Property.forName("lastSyncDate").eq(mostRecentDate)); - - return (OrderSyncInfo) criteria.uniqueResult(); + public OrderSyncInfo findLastSynchronizedInfoByOrderAndConnectorId( + Order order, String connectorId) { + List orderSyncInfoList = findLastSynchronizedInfosByOrderAndConnectorId( + order, connectorId); + if (orderSyncInfoList == null || orderSyncInfoList.isEmpty()) { + return null; + } + return orderSyncInfoList.get(0); } @Override @SuppressWarnings("unchecked") - public List findByOrderLastSynchronizedInfos(Order order) { + public List findLastSynchronizedInfosByOrderAndConnectorId( + Order order, String connectorId) { Criteria criteria = getSession().createCriteria(OrderSyncInfo.class); criteria.add(Restrictions.eq("order", order)); - criteria.add(Restrictions.isNotNull("code")); + criteria.add(Restrictions.eq("connectorId", connectorId)); criteria.addOrder(org.hibernate.criterion.Order.desc("lastSyncDate")); return criteria.list(); } diff --git a/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java b/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java index 7003dc32d..404b7d7f1 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java +++ b/libreplan-business/src/main/java/org/libreplan/business/orders/entities/OrderSyncInfo.java @@ -22,23 +22,37 @@ package org.libreplan.business.orders.entities; import java.util.Date; import org.apache.commons.lang.Validate; +import org.hibernate.validator.NotNull; import org.libreplan.business.common.BaseEntity; /** - * OrderSyncInfo entity + * OrderSyncInfo entity. This entity holds order synchronization info. Each time + * that order synchronization is successfully completed, an instance of this + * entity is created and saved to DB to hold the synchronized info. This info is + * then displayed in UI. + * + * This entity contains the following fields: + *
    + *
  • lastSyncDate: last date where synchronization took place
  • + *
  • key: an identifier, which connector's key is last synchronized
  • + *
  • connectorId: an identifier to distinguish which connector has running the + * synchronization
  • + *
  • order: order that is synchronized
  • + *
* * @author Miciele Ghiorghis */ public class OrderSyncInfo extends BaseEntity { private Date lastSyncDate; - private String label; - private String code; + private String key; + private String connectorId; private Order order; - public static OrderSyncInfo create(Order order) { + public static OrderSyncInfo create(Order order, String connectorId) { Validate.notNull(order); - return create(new OrderSyncInfo(order)); + Validate.notEmpty(connectorId); + return create(new OrderSyncInfo(order, connectorId)); } /** @@ -47,11 +61,13 @@ public class OrderSyncInfo extends BaseEntity { protected OrderSyncInfo() { } - private OrderSyncInfo(Order order) { + private OrderSyncInfo(Order order, String connectorId) { this.lastSyncDate = new Date(); this.order = order; + this.connectorId = connectorId; } + @NotNull(message = "last synchronized date not specified") public Date getLastSyncDate() { return lastSyncDate; } @@ -60,20 +76,22 @@ public class OrderSyncInfo extends BaseEntity { this.lastSyncDate = lastSyncDate; } - public String getLabel() { - return label; + @NotNull(message = "key not specified") + public String getKey() { + return key; } - public void setLabel(String label) { - this.label = label; + public void setKey(String key) { + this.key = key; } - public String getCode() { - return code; + @NotNull(message = "connector id not specified") + public String getConnectorId() { + return connectorId; } - public void setCode(String code) { - this.code = code; + public void setConnectorId(String connectorId) { + this.connectorId = connectorId; } public Order getOrder() { @@ -83,5 +101,4 @@ public class OrderSyncInfo extends BaseEntity { public void setOrder(Order order) { this.order = order; } - } diff --git a/libreplan-business/src/main/resources/db.changelog-1.3.xml b/libreplan-business/src/main/resources/db.changelog-1.3.xml index 217c7296a..1aad14a67 100644 --- a/libreplan-business/src/main/resources/db.changelog-1.3.xml +++ b/libreplan-business/src/main/resources/db.changelog-1.3.xml @@ -309,7 +309,6 @@ Change column jira_labels in configuration to TEXT in MySQL ALTER TABLE configuration MODIFY jira_labels TEXT - Create new table order_sync_info @@ -320,9 +319,15 @@ - - - + + + + + + + + + - - - + + + diff --git a/libreplan-webapp/src/main/java/org/libreplan/importers/ExportTimesheetsToTim.java b/libreplan-webapp/src/main/java/org/libreplan/importers/ExportTimesheetsToTim.java index c4e7a7c60..372bee52b 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/importers/ExportTimesheetsToTim.java +++ b/libreplan-webapp/src/main/java/org/libreplan/importers/ExportTimesheetsToTim.java @@ -88,19 +88,23 @@ public class ExportTimesheetsToTim implements IExportTimesheetsToTim { @Autowired private IOrderDAO orderDAO; + private static final String CONNECTOR_MAJOR_ID = "Tim"; + @Override @Transactional(readOnly = true) public void exportTimesheets() { - Map prop = appPropertiesDAO.findByMajorId("Tim"); + Map prop = appPropertiesDAO + .findByMajorId(CONNECTOR_MAJOR_ID); List orders = orderDAO.getOrders(); for (Order order : orders) { OrderSyncInfo orderSyncInfo = orderSyncInfoDAO - .findByOrderLastSynchronizedInfo(order); + .findLastSynchronizedInfoByOrderAndConnectorId(order, + CONNECTOR_MAJOR_ID); if (orderSyncInfo == null) { LOG.warn("Order '" + order.getName() + "' is not yet synchronized"); } else { - boolean result = exportTimesheets(orderSyncInfo.getCode(), + boolean result = exportTimesheets(orderSyncInfo.getKey(), orderSyncInfo.getOrder(), prop); LOG.info("Export successful: " + result); } @@ -117,7 +121,8 @@ public class ExportTimesheetsToTim implements IExportTimesheetsToTim { throw new RuntimeException("Order should not be empty"); } - Map prop = appPropertiesDAO.findByMajorId("Tim"); + Map prop = appPropertiesDAO + .findByMajorId(CONNECTOR_MAJOR_ID); return exportTimesheets(productCode, order, prop); } @@ -213,9 +218,9 @@ public class ExportTimesheetsToTim implements IExportTimesheetsToTim { .runOnAnotherTransaction(new IOnTransaction() { @Override public Void execute() { - OrderSyncInfo orderSyncInfo = OrderSyncInfo - .create(order); - orderSyncInfo.setCode(productCode); + OrderSyncInfo orderSyncInfo = OrderSyncInfo.create( + order, CONNECTOR_MAJOR_ID); + orderSyncInfo.setKey(productCode); orderSyncInfoDAO.save(orderSyncInfo); return null; } @@ -270,7 +275,8 @@ public class ExportTimesheetsToTim implements IExportTimesheetsToTim { @Override @Transactional(readOnly = true) public OrderSyncInfo getOrderLastSyncInfo(Order order) { - return orderSyncInfoDAO.findByOrderLastSynchronizedInfo(order); + return orderSyncInfoDAO.findLastSynchronizedInfoByOrderAndConnectorId( + order, CONNECTOR_MAJOR_ID); } } diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/orders/TimSynchronizationController.java b/libreplan-webapp/src/main/java/org/libreplan/web/orders/TimSynchronizationController.java index 678e4e699..14ee91290 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/orders/TimSynchronizationController.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/orders/TimSynchronizationController.java @@ -95,7 +95,7 @@ public class TimSynchronizationController extends GenericForwardComposer { .getLastSyncDate())); } if (labelProductCode != null) { - labelProductCode.setValue("(" + orderSyncInfo.getCode() + ")"); + labelProductCode.setValue("(" + orderSyncInfo.getKey() + ")"); } } }