View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.kuali.ole.deliver.bo.*;
4   import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
5   import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
6   import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
7   import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
8   import org.kuali.rice.kim.impl.identity.entity.EntityBo;
9   import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
10  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
11  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
12  import org.kuali.rice.krad.bo.PersistableBusinessObject;
13  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
14  import org.kuali.rice.krad.service.DocumentService;
15  import org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl;
16  import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
17  import org.kuali.rice.krad.util.KRADConstants;
18  import org.kuali.rice.krad.util.ObjectUtils;
19  
20  import java.io.Serializable;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   *OlePatronMaintenanceDocumentServiceImpl generates maintenance object and perform copy operation.
26   */
27  public class OlePatronMaintenanceDocumentServiceImpl extends MaintenanceDocumentServiceImpl implements OlePatronMaintenanceDocumentService {
28      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronMaintenanceDocumentServiceImpl.class);
29  
30  
31      private DocumentService documentService;
32  
33      /**
34       * Gets the value of documentService which is of type DocumentService
35       * @return documentService(DocumentService)
36       */
37      protected DocumentService getDocumentService() {
38          return this.documentService;
39      }
40      /**
41       * Sets the value for documentService which is of type DocumentService
42       * @param documentService(DocumentService)
43       */
44      public void setDocumentService(DocumentService documentService) {
45          this.documentService = documentService;
46      }
47      /**
48       * This method will set the patron object from the request parameters
49       * @param document
50       * @param maintenanceAction
51       * @param requestParameters
52       */
53      @Override
54      public void setupMaintenanceObject(MaintenanceDocument document, String maintenanceAction,
55                                         Map<String, String[]> requestParameters) {
56          LOG.debug("Inside setupMaintenanceObject method");
57          document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
58          document.getOldMaintainableObject().setMaintenanceAction(maintenanceAction);
59  
60          // if action is edit or copy first need to retrieve the old record
61          if (!KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) &&
62                  !KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equals(maintenanceAction)) {
63              Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
64  
65              // enhancement to indicate fields to/not to copy
66              Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
67  
68              // process further object preparations for copy action
69              if (KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
70                  processMaintenanceObjectForCopy(document, newDataObject, requestParameters);
71                  processEntityForCopy(newDataObject);
72              } else {
73                  checkMaintenanceActionAuthorization(document, oldDataObject, maintenanceAction, requestParameters);
74              }
75  
76              // set object instance for editing
77              document.getOldMaintainableObject().setDataObject(oldDataObject);
78              document.getNewMaintainableObject().setDataObject(newDataObject);
79          }
80  
81          if (KRADConstants.MAINTENANCE_NEWWITHEXISTING_ACTION.equals(maintenanceAction)) {
82              Object newBO = document.getNewMaintainableObject().getDataObject();
83              Map<String, String> parameters =
84                      buildKeyMapFromRequest(requestParameters, document.getNewMaintainableObject().getDataObjectClass());
85              ObjectPropertyUtils.copyPropertiesToObject(parameters, newBO);
86              if (newBO instanceof PersistableBusinessObject) {
87                  ((PersistableBusinessObject) newBO).refresh();
88              }
89  
90              document.getNewMaintainableObject().setupNewFromExisting(document, requestParameters);
91          } else if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction)) {
92              document.getNewMaintainableObject().processAfterNew(document, requestParameters);
93          }
94      }
95  
96      /**
97       * @see org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl#setupMaintenanceObject
98       */
99      /**
100      * This method creates maintenance object for delete operation using maintenanceAction.
101      * @param document
102      * @param maintenanceAction
103      * @param requestParameters
104      */
105     public void setupMaintenanceObjectForDelete(MaintenanceDocument document, String maintenanceAction,
106                                                 Map<String, String[]> requestParameters) {
107         document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
108         document.getOldMaintainableObject().setMaintenanceAction(maintenanceAction);
109 
110         Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
111         Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
112 
113         document.getOldMaintainableObject().setDataObject(oldDataObject);
114         document.getNewMaintainableObject().setDataObject(newDataObject);
115     }
116 
117     /**
118      * This method will remove the primary key and the object Id's of the entity object and its child object which is used in copy functionality
119      * @param maintenanceObject
120      */
121     protected void processEntityForCopy(Object maintenanceObject) {
122         LOG.debug("Inside processEntityForCopy method");
123         OlePatronDocument newPatron = (OlePatronDocument) maintenanceObject;
124         newPatron.setOlePatronId(null);
125         boolean  isValueSet=false;
126         EntityBo kimEntity = newPatron.getEntity();
127         if(kimEntity.getNames().size() > 0 ) {
128             List<EntityAffiliationBo> affiliationBos = (List<EntityAffiliationBo>) kimEntity.getAffiliations();
129             for(EntityAffiliationBo affiliationBo : affiliationBos) {
130                 affiliationBo.setId(null);
131                 affiliationBo.setObjectId(null);
132                 affiliationBo.setVersionNumber(null);
133                 affiliationBo.setEntityId(null);
134             }
135             List<EntityEmploymentBo> employmentBos = (List<EntityEmploymentBo>) kimEntity.getEmploymentInformation();
136             for(EntityEmploymentBo employmentBo : employmentBos){
137                 employmentBo.setId(null);
138                 employmentBo.setObjectId(null);
139                 employmentBo.setVersionNumber(null);
140                 employmentBo.setEntityId(null);
141             }
142             EntityNameBo entityName = (EntityNameBo) ObjectUtils.deepCopy((Serializable)kimEntity.getNames().get(0));
143             entityName.setId(null);
144             entityName.setEntityId(null);
145             entityName.setObjectId(null);
146             entityName.setVersionNumber(null);
147             if(kimEntity.getEntityTypeContactInfos().size() > 0) {
148                 EntityTypeContactInfoBo entityContactInfo = (EntityTypeContactInfoBo) ObjectUtils.deepCopy((Serializable)kimEntity.getEntityTypeContactInfos().get(0));
149                 List<EntityPhoneBo> phones = entityContactInfo.getPhoneNumbers();
150                 if(phones.size() > 0) {
151                     for(EntityPhoneBo phone : phones) {
152                         phone.setId(null);
153                         phone.setEntityId(null);
154                         phone.setObjectId(null);
155                         phone.setVersionNumber(null);
156                     }
157                 }
158 
159                 List<EntityEmailBo> emails = entityContactInfo.getEmailAddresses();
160                 if(emails.size() > 0 ) {
161                     for(EntityEmailBo email : emails) {
162                         email.setId(null);
163                         email.setEntityId(null);
164                         email.setObjectId(null);
165                         email.setVersionNumber(null);
166                     }
167                 }
168                 List<EntityAddressBo> addresses = entityContactInfo.getAddresses();
169                 if(addresses.size() > 0) {
170                     for(EntityAddressBo addr : addresses) {
171                         addr.setId(null);
172                         addr.setEntityId(null);
173                         addr.setObjectId(null);
174                         addr.setVersionNumber(null);
175                     }
176                 }
177                 List<OlePatronNotes> notes = newPatron.getNotes();
178                 if(notes.size() > 0) {
179                     for(OlePatronNotes note : notes) {
180                         note.setPatronNoteId(null);
181                         note.setOlePatronId(null);
182                         note.setObjectId(null);
183                         note.setVersionNumber(null);
184                     }
185                 }
186                 newPatron.setPhones(phones);
187                 newPatron.setEmails(emails);
188                 newPatron.setEntity(null);
189                 newPatron.setNotes(notes);
190             }
191             newPatron.setName(entityName);
192             isValueSet=true;
193         } else {
194 
195             EntityBo entityBo = (EntityBo) ObjectUtils.deepCopy((Serializable) newPatron.getEntity());
196             entityBo.setId(null);
197             entityBo.setObjectId(null);
198             entityBo.setVersionNumber(null);
199             List<EntityPhoneBo> phones = newPatron.getPhones();
200             if (phones!=null && phones.size() > 0) {
201                 for (EntityPhoneBo phone : phones) {
202                     phone.setId(null);
203                     phone.setEntityId(null);
204                     phone.setObjectId(null);
205                     phone.setVersionNumber(null);
206                 }
207             }
208 
209             List<EntityEmailBo> emails = newPatron.getEmails();
210             if (emails!=null && emails.size() > 0) {
211                 for (EntityEmailBo email : emails) {
212                     email.setId(null);
213                     email.setEntityId(null);
214                     email.setObjectId(null);
215                     email.setVersionNumber(null);
216                 }
217             }
218             List<EntityAddressBo> addresses = newPatron.getAddresses();
219             if (addresses!=null && addresses.size() > 0) {
220                 for (EntityAddressBo addr : addresses) {
221                     addr.setId(null);
222                     addr.setEntityId(null);
223                     addr.setObjectId(null);
224                     addr.setVersionNumber(null);
225                 }
226             }
227             List<OlePatronNotes> notes = newPatron.getNotes();
228             if (notes!=null && notes.size() > 0) {
229                 for (OlePatronNotes note : notes) {
230                     note.setPatronNoteId(null);
231                     note.setOlePatronId(null);
232                     note.setObjectId(null);
233                     note.setVersionNumber(null);
234                 }
235             }
236 
237             if (!isValueSet) {
238                 newPatron.setPhones(phones);
239                 newPatron.setEmails(emails);
240                 newPatron.setEntity(entityBo);
241                 newPatron.setNotes(notes);
242             }
243         }
244         List<OleProxyPatronDocument> oleProxyPatron = newPatron.getOleProxyPatronDocuments();
245         if (oleProxyPatron.size() > 0) {
246             for (OleProxyPatronDocument proxyPatron : oleProxyPatron) {
247                 proxyPatron.setOleProxyPatronDocumentId(null);
248                 proxyPatron.setOlePatronId(null);
249                 proxyPatron.setObjectId(null);
250                 proxyPatron.setVersionNumber(null);
251             }
252         }
253         newPatron.setOleProxyPatronDocuments(oleProxyPatron);
254 
255         List<EntityEmploymentBo> employmentBoList = newPatron.getEmployments();
256         if(employmentBoList.size() > 0) {
257             for(EntityEmploymentBo employmentBo : employmentBoList ) {
258                 employmentBo.setId(null);
259                 employmentBo.setEntityId(null);
260                 employmentBo.setObjectId(null);
261                 employmentBo.setVersionNumber(null);
262             }
263         }
264         newPatron.setEmployments(employmentBoList);
265         EntityAffiliationBo entityAffiliationBo = new EntityAffiliationBo();
266         List<OlePatronAffiliation> affiliationBoList = newPatron.getPatronAffiliations();
267         //List<EntityAffiliationBo> entityAffiliationBoList = kimEntity.getAffiliations();
268         if(affiliationBoList.size() > 0) {
269             for(OlePatronAffiliation affiliationBo : affiliationBoList) {
270                 affiliationBo.setEntityAffiliationId(null);
271                 affiliationBo.setVersionNumber(null);
272                 affiliationBo.setObjectId(null);
273                 entityAffiliationBo = affiliationBo.getEntityAffliationBo();
274                 entityAffiliationBo.setId(null);
275                 entityAffiliationBo.setEntityId(null);
276                 entityAffiliationBo.setObjectId(null);
277                 entityAffiliationBo.setVersionNumber(null);
278 
279 
280             }
281         }
282         newPatron.setPatronAffiliations(affiliationBoList);
283     }
284 }