View Javadoc
1   package org.kuali.ole.deliver.calendar.service.impl;
2   
3   import org.kuali.ole.deliver.calendar.bo.*;
4   import org.kuali.ole.deliver.calendar.service.OleCalendarDocumentService;
5   import org.kuali.rice.krad.bo.PersistableBusinessObject;
6   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
7   import org.kuali.rice.krad.service.DocumentService;
8   import org.kuali.rice.krad.service.KRADServiceLocator;
9   import org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl;
10  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
11  import org.kuali.rice.krad.util.KRADConstants;
12  import org.kuali.rice.krad.util.ObjectUtils;
13  
14  import java.io.Serializable;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  /**
20   * OleCalendarDocumentServiceImpl generates maintenance object and perform copy operation.
21   */
22  public class OleCalendarDocumentServiceImpl extends MaintenanceDocumentServiceImpl implements OleCalendarDocumentService {
23      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleCalendarDocumentServiceImpl.class);
24  
25  
26      private DocumentService documentService;
27  
28      /**
29       * Gets the value of documentService which is of type DocumentService
30       *
31       * @return documentService(DocumentService)
32       */
33      protected DocumentService getDocumentService() {
34          return this.documentService;
35      }
36  
37      /**
38       * Sets the value for documentService which is of type DocumentService
39       *
40       * @param documentService(DocumentService)
41       *
42       */
43      public void setDocumentService(DocumentService documentService) {
44          this.documentService = documentService;
45      }
46  
47      /**
48       * This method will set the patron object from the request parameters
49       *
50       * @param document
51       * @param maintenanceAction
52       * @param requestParameters
53       */
54      @Override
55      public void setupMaintenanceObject(MaintenanceDocument document, String maintenanceAction,
56                                         Map<String, String[]> requestParameters) {
57          LOG.debug("Inside setupMaintenanceObject method");
58          document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
59          document.getOldMaintainableObject().setMaintenanceAction(maintenanceAction);
60  
61          // if action is edit or copy first need to retrieve the old record
62          if (!KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) &&
63                  !KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equals(maintenanceAction)) {
64              Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
65  
66              // enhancement to indicate fields to/not to copy
67              Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
68  
69              // process further object preparations for copy action
70              if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
71                  processMaintenanceObjectForCopy(document, newDataObject, requestParameters);
72                  processCalendarDocumentForCopy(newDataObject);
73              } else {
74                  checkMaintenanceActionAuthorization(document, oldDataObject, maintenanceAction, requestParameters);
75              }
76  
77              // set object instance for editing
78              document.getOldMaintainableObject().setDataObject(oldDataObject);
79              document.getNewMaintainableObject().setDataObject(newDataObject);
80          }
81  
82          if (KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equals(maintenanceAction)) {
83              Object newBO = document.getNewMaintainableObject().getDataObject();
84              Map<String, String> parameters =
85                      buildKeyMapFromRequest(requestParameters, document.getNewMaintainableObject().getDataObjectClass());
86              ObjectPropertyUtils.copyPropertiesToObject(parameters, newBO);
87              if (newBO instanceof PersistableBusinessObject) {
88                  ((PersistableBusinessObject) newBO).refresh();
89              }
90  
91              document.getNewMaintainableObject().setupNewFromExisting(document, requestParameters);
92          } else if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
93              document.getNewMaintainableObject().processAfterNew(document, requestParameters);
94          }
95      }
96  
97      /**
98       * @see org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl#setupMaintenanceObject
99       */
100     /**
101      * This method creates maintenance object for delete operation using maintenanceAction.
102      *
103      * @param document
104      * @param maintenanceAction
105      * @param requestParameters
106      */
107     public void setupMaintenanceObjectForDelete(MaintenanceDocument document, String maintenanceAction,
108                                                 Map<String, String[]> requestParameters) {
109         document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
110         document.getOldMaintainableObject().setMaintenanceAction(maintenanceAction);
111 
112         Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
113         Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
114 
115         document.getOldMaintainableObject().setDataObject(oldDataObject);
116         document.getNewMaintainableObject().setDataObject(newDataObject);
117     }
118 
119     /**
120      * This method used to delete all the list of objects and save the latest collection in OleCalendar maintenance document.
121      *
122      * @param oleCalendar
123      * @return
124      */
125     public OleCalendar getNewCalendar(OleCalendar oleCalendar) {
126         if (oleCalendar.getCalendarId() != null && !oleCalendar.getCalendarId().isEmpty()) {
127             HashMap<String, String> calendarMap = new HashMap<>();
128             calendarMap.put("calendarId", oleCalendar.getCalendarId());
129             OleCalendar tempCalendar = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleCalendar.class, calendarMap);
130             if (tempCalendar != null) {
131                 if (tempCalendar.getOleCalendarExceptionPeriodList().size() > 0) {
132                     for (OleCalendarExceptionPeriod oleCalendarExceptionPeriod : tempCalendar.getOleCalendarExceptionPeriodList()) {
133                         if (oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList().size() > 0) {
134                             KRADServiceLocator.getBusinessObjectService().delete(tempCalendar.getOleCalendarExceptionPeriodList());
135                         }
136                     }
137                     KRADServiceLocator.getBusinessObjectService().delete(tempCalendar.getOleCalendarExceptionPeriodList());
138                 }
139                 if (tempCalendar.getOleCalendarWeekList().size() > 0) {
140                     KRADServiceLocator.getBusinessObjectService().delete(tempCalendar.getOleCalendarWeekList());
141                 }
142                 if (tempCalendar.getOleCalendarExceptionDateList().size() > 0) {
143                     KRADServiceLocator.getBusinessObjectService().delete(tempCalendar.getOleCalendarExceptionDateList());
144                 }
145 
146             }
147         }
148         return oleCalendar;
149     }
150 
151     /**
152      * This method will remove the primary key and the object Id's of the calendar object and its child object which is used in copy functionality
153      * @param maintenanceObject
154      */
155     protected void processCalendarDocumentForCopy(Object maintenanceObject) {
156         LOG.debug("Inside processCalendarDocumentForCopy method");
157         OleCalendar newCalendar = (OleCalendar) maintenanceObject;
158         List<OleCalendarWeek> oleCalendarWeekList = newCalendar.getOleCalendarWeekList();
159         if(oleCalendarWeekList.size() > 0) {
160             for(OleCalendarWeek oleCalendarWeek : oleCalendarWeekList) {
161                 oleCalendarWeek.setCalendarWeekId(null);
162                 oleCalendarWeek.setCalendarId(null);
163                 oleCalendarWeek.setVersionNumber(null);
164                 oleCalendarWeek.setObjectId(null);
165             }
166         }
167         List<OleCalendarExceptionPeriod> oleCalendarExceptionPeriodList = newCalendar.getOleCalendarExceptionPeriodList();
168         if(oleCalendarExceptionPeriodList.size() > 0) {
169             for(OleCalendarExceptionPeriod oleCalendarExceptionPeriod : oleCalendarExceptionPeriodList) {
170                 for(OleCalendarExceptionPeriodWeek oleCalendarExceptionPeriodWeek : oleCalendarExceptionPeriod.getOleCalendarExceptionPeriodWeekList()) {
171                     {
172                         oleCalendarExceptionPeriodWeek.setCalendarWeekId(null);
173                         oleCalendarExceptionPeriodWeek.setCalendarExceptionPeriodId(null);
174                         oleCalendarExceptionPeriodWeek.setVersionNumber(null);
175                         oleCalendarExceptionPeriodWeek.setObjectId(null);
176                     }
177                 oleCalendarExceptionPeriod.setCalendarExceptionPeriodId(null);
178                 oleCalendarExceptionPeriod.setCalendarId(null);
179                 oleCalendarExceptionPeriod.setVersionNumber(null);
180                 oleCalendarExceptionPeriod.setObjectId(null);
181                 }
182             }
183         }
184         List<OleCalendarExceptionDate> oleCalendarExceptionDateList = newCalendar.getOleCalendarExceptionDateList();
185         if(oleCalendarExceptionDateList.size() > 0) {
186             for(OleCalendarExceptionDate oleCalendarExceptionDate : oleCalendarExceptionDateList) {
187                 oleCalendarExceptionDate.setCalendarExceptionDateId(null);
188                 oleCalendarExceptionDate.setCalendarId(null);
189                 oleCalendarExceptionDate.setVersionNumber(null);
190                 oleCalendarExceptionDate.setObjectId(null);
191             }
192         }
193 
194     }
195 }