View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.ingest.pojo.OleLocationGroup;
5   import org.kuali.ole.ingest.pojo.OleLocationIngest;
6   import org.kuali.ole.describe.bo.OleLocation;
7   import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
8   import org.kuali.rice.kew.api.exception.WorkflowException;
9   import org.kuali.rice.kim.api.identity.IdentityService;
10  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
11  import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
12  import org.kuali.rice.krad.service.BusinessObjectService;
13  import org.kuali.rice.krad.service.DocumentService;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  
16  import java.io.IOException;
17  import java.net.URISyntaxException;
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  /**
24   * OleLocationServiceImpl generates list of location to perform location operation using xml content.
25   */
26  public class OleLocationServiceImpl implements OleLocationService {
27  
28       private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleLocationServiceImpl.class);
29  
30      private BusinessObjectService businessObjectService;
31      private IdentityService identityService;
32      private DocumentService documentService;
33      private OleLocationConverterService oleLocationConverterService;
34      private List<OleLocationIngest> createLocationList = new ArrayList<OleLocationIngest>();
35      private List<OleLocationIngest> updateLocationList = new ArrayList<OleLocationIngest>();
36  
37      /**
38       * Gets the instance of BusinessObjectService
39       * @return businessObjectService(BusinessObjectService)
40       */
41      public BusinessObjectService getBusinessObjectService() {
42          if (null == businessObjectService) {
43              businessObjectService = KRADServiceLocator.getBusinessObjectService();
44          }
45          return businessObjectService;
46      }
47  
48      /**
49       *   Gets the instance of IdentityService
50       * @param serviceName
51       * @param <T>
52       * @return <T extends Object>
53       */
54      protected static <T extends Object> T getService(String serviceName) {
55             return GlobalResourceLoader.<T>getService(serviceName);
56         }
57  
58      /**
59       *  Sets the businessObjectService attribute value.
60       * @param businessObjectService
61       */
62      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
63          this.businessObjectService = businessObjectService;
64      }
65  
66      /**
67       * Gets the identityService attribute value.
68       * @return  identityService
69       */
70      public IdentityService getIdentityService() {
71          return identityService;
72      }
73  
74      /**
75       *  Sets the identityService attribute value.
76       * @param identityService
77       */
78      public void setIdentityService(IdentityService identityService) {
79          this.identityService = identityService;
80      }
81  
82      /**
83       * This method returns documentService
84       * @return DocumentService
85       */
86      public DocumentService getDocumentService() {
87          return getService(OLEConstants.DOCUMENT_HEADER_SERVICE);
88      }
89  
90      /**
91       *  Sets the documentService attribute value.
92       * @param documentService
93       */
94      public void setDocumentService(DocumentService documentService) {
95          this.documentService = documentService;
96      }
97  
98      /**
99       *  This method returns location using locationCode.
100      * @param locationCode
101      * @return OleLocation
102      */
103     @Override
104     public OleLocation getLocation(String locationCode) {
105         LOG.debug("Inside the getLocation method");
106         Map<String,Object> criteria = new HashMap<String,Object>(4);
107         criteria.put(OLEConstants.PATRON_ENTITY_ACTIVE, Boolean.TRUE);
108         return businessObjectService.findByPrimaryKey(OleLocation.class, criteria);
109     }
110 
111     /**
112      *  This method saves location document.
113      * @param locationDocument
114      * @return  locationDocument.getDocumentNumber
115      * @throws org.kuali.rice.kew.api.exception.WorkflowException
116      */
117     private String saveLocationDocument(MaintenanceDocument locationDocument) throws WorkflowException {
118         getDocumentService().saveDocument(locationDocument, SaveDocumentEvent.class);
119         return locationDocument.getDocumentNumber();
120     }
121 
122     /**
123      * This method returns location number once location document is routed.
124      * @param locationDocument
125      * @return   locationDocument.getDocumentNumber
126      * @throws org.kuali.rice.kew.api.exception.WorkflowException
127      */
128     private String routeLocationDocument(MaintenanceDocument locationDocument) throws WorkflowException{
129         getDocumentService().routeDocument(locationDocument, null, null);
130         return locationDocument.getDocumentNumber();
131     }
132 
133     /**
134      * This method returns boolean after create location.
135      * @param oleLocation
136      * @return  doc
137      */
138     public boolean createLocation(OleLocation oleLocation) {
139        boolean doc = false;
140        OleLocation oleLocation1= getBusinessObjectService().save(oleLocation);
141        doc= true;
142        return doc;
143     }
144 
145     /**
146      *  This method updates location using oleLocation.
147      * @param oleLocation
148      * @return   doc
149      */
150     public boolean updateLocation(OleLocation oleLocation) {
151        boolean doc = false;
152        OleLocation oleLocation1= getBusinessObjectService().save(oleLocation);
153        doc= true;
154        return doc;
155      }
156 
157 
158     /**
159      *  This method process the location details using xmlContent.This updates location if already exist in the list otherwise
160      *  creates new location list.
161      * @param xmlContent
162      * @throws java.io.IOException
163      * @throws java.net.URISyntaxException
164      */
165     private void processLocationDetails(String xmlContent) throws IOException, URISyntaxException {
166 
167         OleLocationGroup oleLocationGroup =oleLocationConverterService.buildLocationFromFileContent(xmlContent);
168         List<OleLocation> existingLocationList = (List<OleLocation>) getBusinessObjectService().findAll(OleLocation.class);
169                  String oleLocationCode ;
170                   OleLocationIngest oleLocationIngest;
171 
172        for(int i = 0;i<oleLocationGroup.getLocationGroup().size();i++){
173               oleLocationIngest = oleLocationGroup.getLocationGroup().get(i);
174               oleLocationCode = oleLocationIngest.getLocationCode();
175              for(int j=0;j<existingLocationList.size();j++){
176                  if(oleLocationCode.equals(existingLocationList.get(j).getLocationCode())){
177                      updateLocationList.add(oleLocationIngest);
178                  }else if(!(oleLocationCode == null) || !(oleLocationCode.equals(""))){
179                      createLocationList.add(oleLocationIngest);
180                  }
181              }
182        }
183     }
184   }
185