View Javadoc
1   package org.kuali.ole.deliver.calendar.inquiry;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.calendar.bo.*;
5   import org.kuali.ole.deliver.calendar.service.DateUtil;
6   import org.kuali.rice.krad.bo.BusinessObject;
7   import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
8   import org.kuali.rice.krad.datadictionary.exception.UnknownBusinessClassAttributeException;
9   import org.kuali.rice.krad.inquiry.InquirableImpl;
10  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
11  import org.kuali.rice.krad.service.ModuleService;
12  
13  import java.security.GeneralSecurityException;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  
18  /**
19   * Created with IntelliJ IDEA.
20   * User: dileepp
21   * Date: 8/26/13
22   * Time: 4:17 PM
23   * To change this template use File | Settings | File Templates.
24   */
25  public class OleCalendarInquirableImpl extends InquirableImpl {
26  
27      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleCalendarInquirableImpl.class);
28  
29      public Object retrieveDataObject(Map<String, String> parameters) {
30          if (dataObjectClass == null) {
31              LOG.error("Data object class must be set in inquirable before retrieving the object");
32              throw new RuntimeException("Data object class must be set in inquirable before retrieving the object");
33          }
34  
35          // build list of key values from the map parameters
36          List<String> pkPropertyNames = getDataObjectMetaDataService().listPrimaryKeyFieldNames(dataObjectClass);
37  
38          // some classes might have alternate keys defined for retrieving
39          List<List<String>> alternateKeyNames = this.getAlternateKeysForClass(dataObjectClass);
40  
41          // add pk set as beginning so it will be checked first for match
42          alternateKeyNames.add(0, pkPropertyNames);
43  
44          List<String> dataObjectKeySet = retrieveKeySetFromMap(alternateKeyNames, parameters);
45          if ((dataObjectKeySet == null) || dataObjectKeySet.isEmpty()) {
46              LOG.warn("Matching key set not found in request for class: " + getDataObjectClass());
47  
48              return null;
49          }
50  
51          // found key set, now build map of key values pairs we can use to retrieve the object
52          Map<String, Object> keyPropertyValues = new HashMap<String, Object>();
53          for (String keyPropertyName : dataObjectKeySet) {
54              String keyPropertyValue = parameters.get(keyPropertyName);
55  
56              // uppercase value if needed
57              Boolean forceUppercase = Boolean.FALSE;
58              try {
59                  forceUppercase = getDataDictionaryService().getAttributeForceUppercase(dataObjectClass,
60                          keyPropertyName);
61              } catch (UnknownBusinessClassAttributeException ex) {
62                  // swallowing exception because this check for ForceUppercase would
63                  // require a DD entry for the attribute, and we will just set force uppercase to false
64                  LOG.warn("Data object class "
65                          + dataObjectClass
66                          + " property "
67                          + keyPropertyName
68                          + " should probably have a DD definition.", ex);
69              }
70  
71              if (forceUppercase.booleanValue() && (keyPropertyValue != null)) {
72                  keyPropertyValue = keyPropertyValue.toUpperCase();
73              }
74  
75              // check security on key field
76              if (getDataObjectAuthorizationService().attributeValueNeedsToBeEncryptedOnFormsAndLinks(dataObjectClass,
77                      keyPropertyName)) {
78                  try {
79                      keyPropertyValue = getEncryptionService().decrypt(keyPropertyValue);
80                  } catch (GeneralSecurityException e) {
81                      LOG.error("Data object class "
82                              + dataObjectClass
83                              + " property "
84                              + keyPropertyName
85                              + " should have been encrypted, but there was a problem decrypting it.", e);
86                      throw new RuntimeException("Data object class "
87                              + dataObjectClass
88                              + " property "
89                              + keyPropertyName
90                              + " should have been encrypted, but there was a problem decrypting it.", e);
91                  }
92              }
93  
94              keyPropertyValues.put(keyPropertyName, keyPropertyValue);
95          }
96  
97          // now retrieve the object based on the key set
98          Object dataObject = null;
99  
100         ModuleService moduleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(
101                 getDataObjectClass());
102         if (moduleService != null && moduleService.isExternalizable(getDataObjectClass())) {
103             dataObject = moduleService.getExternalizableBusinessObject(getDataObjectClass().asSubclass(
104                     ExternalizableBusinessObject.class), keyPropertyValues);
105         } else if (BusinessObject.class.isAssignableFrom(getDataObjectClass())) {
106             dataObject = getBusinessObjectService().findByPrimaryKey(getDataObjectClass().asSubclass(
107                     BusinessObject.class), keyPropertyValues);
108         }
109         OleCalendar oleCalendar = new OleCalendar();
110         try {
111             oleCalendar = (OleCalendar) dataObject;
112             DateUtil dateUtil = new DateUtil();
113             // oleCalendar = (OleCalendar) document.getDocumentDataObject();//changed for edit purpose
114             for (OleCalendarWeek oldCalendarWeek : oleCalendar.getOleCalendarWeekList()) {
115 
116 
117                 if (oldCalendarWeek.getOpenTime().equals("") || oldCalendarWeek.getCloseTime().equals("")) {
118                     break;
119                 }
120                 String convertedOpenTime = "";
121                 String convertCloseTime = "";
122                 String oleOpenTime = oldCalendarWeek.getOpenTime();
123                 String oleCloseTime = oldCalendarWeek.getCloseTime();
124                 try {
125                     convertedOpenTime = dateUtil.convertTo12HoursFormat(oleOpenTime);
126                     convertCloseTime = dateUtil.convertTo12HoursFormat(oleCloseTime);
127                 } catch (java.text.ParseException e) {
128                     LOG.error("Exception while converting to 12Hours format", e);  //To change body of catch statement use File | Settings | File Templates.
129                 }
130 
131                 if (convertedOpenTime != null) {
132                     oldCalendarWeek.setOpenTime(convertedOpenTime.substring(0, convertedOpenTime.length() - 2));
133                 }
134                 if (convertCloseTime != null) {
135                     oldCalendarWeek.setCloseTime(convertCloseTime.substring(0, convertCloseTime.length() - 2));
136                 }
137             }
138             oleCalendar.sortCalendarWeek(oleCalendar); //added for OLE-5381
139             for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
140                 for (OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek : oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()) {
141                     String convOpenTimeExcptPrdWk = "";
142                     String convCloseTimeExcptPrdWk = "";
143                     String oleOpenTimeExcptPrdWk = oleCalendarExceptionPeriodWeek.getOpenTime();
144                     String oleCloseTimeExcptPrdWk = oleCalendarExceptionPeriodWeek.getCloseTime();
145                     try {
146                         convOpenTimeExcptPrdWk = dateUtil.convertTo12HoursFormat(oleOpenTimeExcptPrdWk);
147                         convCloseTimeExcptPrdWk = dateUtil.convertTo12HoursFormat(oleCloseTimeExcptPrdWk);
148                     } catch (java.text.ParseException e) {
149                         LOG.error("Exception while converting to 12Hours format", e);  //To change body of catch statement use File | Settings | File Templates.
150                     }
151 
152                     if (convOpenTimeExcptPrdWk != null) {
153                         oleCalendarExceptionPeriodWeek.setOpenTime(convOpenTimeExcptPrdWk.substring(0, convOpenTimeExcptPrdWk.length() - 2));
154                     }
155                     if (convCloseTimeExcptPrdWk != null) {
156                         oleCalendarExceptionPeriodWeek.setCloseTime(convCloseTimeExcptPrdWk.substring(0, convCloseTimeExcptPrdWk.length() - 2));
157                     }
158                 }
159             }
160 
161 
162             for (OleCalendarExceptionDate oleCalendarExceptionDate : oleCalendar.getOleCalendarExceptionDateList()) {
163                 String convertedOpenTimeExcptDate = "";
164                 String convertedCloseTimeExcptDate = "";
165                 String oleOpenTimeExcptDate = oleCalendarExceptionDate.getOpenTime();
166                 String oleCloseTimeExcptDate = oleCalendarExceptionDate.getCloseTime();
167                 if ((!oleCalendarExceptionDate.getOpenTime().equals("")) && (!oleCalendarExceptionDate.getCloseTime().equals(""))) { //changed
168                     try {
169                         convertedOpenTimeExcptDate = dateUtil.convertTo12HoursFormat(oleOpenTimeExcptDate);
170                         convertedCloseTimeExcptDate = dateUtil.convertTo12HoursFormat(oleCloseTimeExcptDate);
171                     } catch (java.text.ParseException e) {
172                         LOG.error("Exception while converting to 12Hours format", e);  //To change body of catch statement use File | Settings | File Templates.
173                     }
174 
175                     if (convertedOpenTimeExcptDate != null) {
176                         oleCalendarExceptionDate.setOpenTime(convertedOpenTimeExcptDate.substring(0, convertedOpenTimeExcptDate.length() - 2));
177                     }
178                     if (convertedCloseTimeExcptDate != null) {
179                         oleCalendarExceptionDate.setCloseTime(convertedCloseTimeExcptDate.substring(0, convertedCloseTimeExcptDate.length() - 2));
180                     }
181                 }
182             }
183         } catch (Exception e) {
184             LOG.error("Exception", e);
185         }
186 
187         for (OleCalendarWeek oleCalendarWeek : oleCalendar.getOleCalendarWeekList()) {
188             oleCalendarWeek.setStartDay(weekDayConversion(oleCalendarWeek.getStartDay()));
189             oleCalendarWeek.setEndDay(weekDayConversion(oleCalendarWeek.getEndDay()));
190         }
191 
192 
193         for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendar.getOleCalendarExceptionPeriodList()) {
194             for (OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek : oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()) {
195 
196                 oleCalendarExceptionPeriodWeek.setStartDay(weekDayConversion(oleCalendarExceptionPeriodWeek.getStartDay()));
197                 oleCalendarExceptionPeriodWeek.setEndDay(weekDayConversion(oleCalendarExceptionPeriodWeek.getEndDay()));
198             }
199 
200         }
201 
202         return oleCalendar;
203     }
204 
205     private String weekDayConversion(String key) {
206         HashMap<String, String> weekDays = new HashMap<String, String>();
207         weekDays.put("0", OLEConstants.CALENDER_SUN);
208         weekDays.put("1", OLEConstants.CALENDER_MON);
209         weekDays.put("2", OLEConstants.CALENDER_TUE);
210         weekDays.put("3", OLEConstants.CALENDER_WED);
211         weekDays.put("4", OLEConstants.CALENDER_THU);
212         weekDays.put("5", OLEConstants.CALENDER_FRI);
213         weekDays.put("6", OLEConstants.CALENDER_SAT);
214 
215         return weekDays.get(key);
216 
217     }
218 }