001    package org.kuali.ole.service;
002    
003    import org.kuali.ole.OLEConstants;
004    import org.kuali.ole.ingest.pojo.OleLocationGroup;
005    import org.kuali.ole.ingest.pojo.OleLocationIngest;
006    import org.kuali.ole.location.bo.OleLocation;
007    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
008    import org.kuali.rice.kew.api.exception.WorkflowException;
009    import org.kuali.rice.kim.api.identity.IdentityService;
010    import org.kuali.rice.krad.maintenance.MaintenanceDocument;
011    import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
012    import org.kuali.rice.krad.service.BusinessObjectService;
013    import org.kuali.rice.krad.service.DocumentService;
014    import org.kuali.rice.krad.service.KRADServiceLocator;
015    
016    import java.io.IOException;
017    import java.net.URISyntaxException;
018    import java.util.ArrayList;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    
023    /**
024     * OleLocationServiceImpl generates list of location to perform location operation using xml content.
025     */
026    public class OleLocationServiceImpl implements OleLocationService {
027    
028         private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleLocationServiceImpl.class);
029    
030        private BusinessObjectService businessObjectService;
031        private IdentityService identityService;
032        private DocumentService documentService;
033        private OleLocationConverterService oleLocationConverterService;
034        private List<OleLocationIngest> createLocationList = new ArrayList<OleLocationIngest>();
035        private List<OleLocationIngest> updateLocationList = new ArrayList<OleLocationIngest>();
036    
037        /**
038         * Gets the instance of BusinessObjectService
039         * @return businessObjectService(BusinessObjectService)
040         */
041        public BusinessObjectService getBusinessObjectService() {
042            if (null == businessObjectService) {
043                businessObjectService = KRADServiceLocator.getBusinessObjectService();
044            }
045            return businessObjectService;
046        }
047    
048        /**
049         *   Gets the instance of IdentityService
050         * @param serviceName
051         * @param <T>
052         * @return <T extends Object>
053         */
054        protected static <T extends Object> T getService(String serviceName) {
055               return GlobalResourceLoader.<T>getService(serviceName);
056           }
057    
058        /**
059         *  Sets the businessObjectService attribute value.
060         * @param businessObjectService
061         */
062        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
063            this.businessObjectService = businessObjectService;
064        }
065    
066        /**
067         * Gets the identityService attribute value.
068         * @return  identityService
069         */
070        public IdentityService getIdentityService() {
071            return identityService;
072        }
073    
074        /**
075         *  Sets the identityService attribute value.
076         * @param identityService
077         */
078        public void setIdentityService(IdentityService identityService) {
079            this.identityService = identityService;
080        }
081    
082        /**
083         * This method returns documentService
084         * @return DocumentService
085         */
086        public DocumentService getDocumentService() {
087            return getService(OLEConstants.DOCUMENT_HEADER_SERVICE);
088        }
089    
090        /**
091         *  Sets the documentService attribute value.
092         * @param documentService
093         */
094        public void setDocumentService(DocumentService documentService) {
095            this.documentService = documentService;
096        }
097    
098        /**
099         *  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 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 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 IOException
163         * @throws 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