[OBKMO] Handle NonUniqueResultException using uniqueResult().
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Tue, 08 Mar 2011 15:11:37 +0100
changeset 11094 894a91f93223
parent 11093 a1f65b603342
child 11095 83f5834158f1
[OBKMO] Handle NonUniqueResultException using uniqueResult().
modules/org.openbravo.client.myob/src/org/openbravo/client/myob/URLWidgetProvider.java
--- a/modules/org.openbravo.client.myob/src/org/openbravo/client/myob/URLWidgetProvider.java	Tue Mar 08 13:49:44 2011 +0100
+++ b/modules/org.openbravo.client.myob/src/org/openbravo/client/myob/URLWidgetProvider.java	Tue Mar 08 15:11:37 2011 +0100
@@ -21,6 +21,7 @@
 import org.apache.log4j.Logger;
 import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONObject;
+import org.hibernate.NonUniqueResultException;
 import org.hibernate.criterion.Expression;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.dal.service.OBDao;
@@ -53,12 +54,17 @@
       final JSONObject jsonObject = super.getWidgetClassDefinition();
       final JSONObject parameters = new JSONObject();
       jsonObject.put(WidgetProvider.PARAMETERS, parameters);
-      final WidgetURL widgetURL = (WidgetURL) OBDao.getFilteredCriteria(WidgetURL.class,
-          Expression.eq(WidgetURL.PROPERTY_WIDGETCLASS, getWidgetClass())).uniqueResult();
-      if (widgetURL != null) {
-        parameters.put(SRC, widgetURL.getURL());
-      } else {
-        log.warn("URLWidget does not have a URL defined.");
+      try {
+        final WidgetURL widgetURL = (WidgetURL) OBDao.getFilteredCriteria(WidgetURL.class,
+            Expression.eq(WidgetURL.PROPERTY_WIDGETCLASS, getWidgetClass())).uniqueResult();
+        if (widgetURL != null) {
+          parameters.put(SRC, widgetURL.getURL());
+        } else {
+          log.warn("URLWidget does not have a URL defined.");
+          parameters.put(SRC, "");
+        }
+      } catch (NonUniqueResultException e) {
+        log.warn("URLWidget has more than one active URL defined.", e);
         parameters.put(SRC, "");
       }
       if (jsonObject.getJSONArray(WidgetProvider.FIELDDEFINITIONS).length() > 0) {
@@ -72,8 +78,8 @@
   }
 
   public JSONObject getWidgetInstanceDefinition(WidgetInstance widgetInstance) {
+    final JSONObject jsonObject = new JSONObject();
     try {
-      final JSONObject jsonObject = new JSONObject();
       addDefaultWidgetProperties(jsonObject, widgetInstance);
       final JSONObject parameters = jsonObject.getJSONObject(WidgetProvider.PARAMETERS);
       final WidgetURL widgetURL = (WidgetURL) OBDao.getFilteredCriteria(WidgetURL.class,
@@ -83,10 +89,12 @@
       } else {
         log.error("No url widget defined for widget class " + widgetInstance.getWidgetClass());
       }
-      return jsonObject;
+    } catch (NonUniqueResultException e) {
+      log.error("More than one active url defined for widget " + widgetInstance.getWidgetClass(), e);
     } catch (Exception e) {
       throw new OBException(e);
     }
+    return jsonObject;
   }
 
   @Override