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
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
30
31
32
33 protected DocumentService getDocumentService() {
34 return this.documentService;
35 }
36
37
38
39
40
41
42
43 public void setDocumentService(DocumentService documentService) {
44 this.documentService = documentService;
45 }
46
47
48
49
50
51
52
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
62 if (!KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) &&
63 !KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equals(maintenanceAction)) {
64 Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
65
66
67 Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
68
69
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
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
99
100
101
102
103
104
105
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
121
122
123
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
153
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 }