Fixes issue 22293: Wrong checking in c_period_trg3 trigger
authorJavier Etxarri <javier.echarri@openbravo.com>
Mon, 28 Jan 2013 13:55:25 +0100
changeset 19483 f0be705eaf76
parent 19482 06da080dd39f
child 19484 8917ff30b35c
Fixes issue 22293: Wrong checking in c_period_trg3 trigger
src-db/database/sourcedata/AD_MESSAGE.xml
src/org/openbravo/Event/PeriodEnventHandler.java
--- a/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Jan 28 13:03:19 2013 +0100
+++ b/src-db/database/sourcedata/AD_MESSAGE.xml	Mon Jan 28 13:55:25 2013 +0100
@@ -18824,6 +18824,17 @@
 <!--8EA0254289CA4EB291AAB8AD6D317C6F-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--8EA0254289CA4EB291AAB8AD6D317C6F--></AD_MESSAGE>
 
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A--><AD_MESSAGE>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <AD_MESSAGE_ID><![CDATA[8EB1FC8AD95146EC885D2E0D4D92F43A]]></AD_MESSAGE_ID>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <VALUE><![CDATA[DatesOverlappedParams]]></VALUE>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <MSGTEXT><![CDATA[Saving failed. The dates period defined is overlapped with another period of an existing year. The name of the overlaped period is %1 and it exists in year %2.]]></MSGTEXT>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <MSGTYPE><![CDATA[E]]></MSGTYPE>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--8EB1FC8AD95146EC885D2E0D4D92F43A--></AD_MESSAGE>
+
 <!--90227390063C4D1BA7187A2F12C6A55A--><AD_MESSAGE>
 <!--90227390063C4D1BA7187A2F12C6A55A-->  <AD_MESSAGE_ID><![CDATA[90227390063C4D1BA7187A2F12C6A55A]]></AD_MESSAGE_ID>
 <!--90227390063C4D1BA7187A2F12C6A55A-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src/org/openbravo/Event/PeriodEnventHandler.java	Mon Jan 28 13:03:19 2013 +0100
+++ b/src/org/openbravo/Event/PeriodEnventHandler.java	Mon Jan 28 13:55:25 2013 +0100
@@ -28,44 +28,36 @@
   }
 
   public void onUpdate(@Observes EntityUpdateEvent event) {
-    ConnectionProvider conn = new DalConnectionProvider(false);
-    String language = OBContext.getOBContext().getLanguage().getLanguage();
     if (!isValidEvent(event)) {
       return;
     }
-    if (checkPeriod((Period) event.getTargetInstance())) {
-      logger.error("Period " + event.getTargetInstance().getId()
-          + " is being updated and is overlapping another period");
-      throw new OBException(Utility.messageBD(conn, "DatesOverlapped", language));
-    }
-    logger.info("Period " + event.getTargetInstance().getId() + " is being updated");
+    checkPeriod((Period) event.getTargetInstance());
   }
 
   public void onSave(@Observes EntityNewEvent event) {
-    ConnectionProvider conn = new DalConnectionProvider(false);
-    String language = OBContext.getOBContext().getLanguage().getLanguage();
     if (!isValidEvent(event)) {
       return;
     }
-    if (checkPeriod((Period) event.getTargetInstance())) {
-      logger.error("Period " + event.getTargetInstance().getId()
-          + " is being saved and is overlapping another period");
-      throw new OBException(Utility.messageBD(conn, "DatesOverlapped", language));
-    }
-    logger.info("Period " + event.getTargetInstance().getId() + " is being saved");
+    checkPeriod((Period) event.getTargetInstance());
   }
 
-  private boolean checkPeriod(Period period) {
+  private void checkPeriod(Period period) {
+    ConnectionProvider conn = new DalConnectionProvider(false);
+    String language = OBContext.getOBContext().getLanguage().getLanguage();
     OBCriteria<Period> criteria = OBDal.getInstance().createCriteria(Period.class);
     criteria.add(Restrictions.eq(Period.PROPERTY_ORGANIZATION, period.getOrganization()));
     criteria.add(Restrictions.eq(Period.PROPERTY_CLIENT, period.getClient()));
     criteria.add(Restrictions.ne(Period.PROPERTY_ID, period.getId()));
     criteria.add(Restrictions.ge(Period.PROPERTY_ENDINGDATE, period.getStartingDate()));
     criteria.add(Restrictions.le(Period.PROPERTY_STARTINGDATE, period.getEndingDate()));
-    if (criteria.list().size() > 0) {
-      return true;
-    } else {
-      return false;
+    criteria.setMaxResults(1);
+
+    if (criteria.uniqueResult() != null) {
+      logger
+          .error("Period " + period.getId() + " is being saved and is overlapping another period");
+      throw new OBException(Utility.messageBD(conn, "DatesOverlappedParams", language)
+          .replace("%1", ((Period) criteria.uniqueResult()).getName())
+          .replace("%2", ((Period) criteria.uniqueResult()).getYear().getFiscalYear()));
     }
   }
 }
\ No newline at end of file