001package org.kuali.ole.service.impl;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.select.bo.OleEventLogBo;
005import org.kuali.ole.select.bo.OleLicenseRequestBo;
006import org.kuali.ole.select.bo.OleLicenseRequestItemTitle;
007import org.kuali.ole.select.bo.OleLicenseRequestStatus;
008import org.kuali.ole.service.OleLicenseRequestService;
009import org.kuali.ole.service.OleLicenseRequestWebService;
010import org.kuali.rice.core.api.CoreApiServiceLocator;
011import org.kuali.rice.core.api.config.property.ConfigContext;
012import org.kuali.rice.core.api.criteria.CriteriaLookupService;
013import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
014import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
015import org.kuali.rice.kim.api.identity.IdentityService;
016import org.kuali.rice.kim.api.services.KimApiServiceLocator;
017import org.kuali.rice.krad.UserSession;
018import org.kuali.rice.krad.maintenance.MaintenanceDocument;
019import org.kuali.rice.krad.service.BusinessObjectService;
020import org.kuali.rice.krad.service.DocumentService;
021import org.kuali.rice.krad.service.KRADServiceLocator;
022import org.kuali.rice.krad.util.GlobalVariables;
023
024import java.util.*;
025
026/**
027 * OleLicenseRequestWebServiceImpl creates license request for maintenanceDocument.
028 */
029public class OleLicenseRequestWebServiceImpl implements OleLicenseRequestWebService {
030    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleLicenseRequestWebServiceImpl.class);
031    private BusinessObjectService businessObjectService;
032    private IdentityService identityService;
033    private CriteriaLookupService criteriaLookupService;
034    private OleLicenseRequestService oleLicenseRequestService;
035
036    /**
037     *   Returns the businessObjectService instance.
038     *   If it is not null  returns existing businessObjectService else creates new
039     * @return  businessObjectService
040     */
041    protected BusinessObjectService getBusinessObjectService() {
042        if (businessObjectService == null) {
043            businessObjectService = KRADServiceLocator.getBusinessObjectService();
044        }
045        return businessObjectService;
046    }
047
048    /**
049     *  Returns identityService instance .
050     *  If it is not null returns existing instance else creates new instance.
051     * @return  identityService
052     */
053    protected IdentityService getIdentityService() {
054        if (identityService == null) {
055            identityService = KimApiServiceLocator.getIdentityService();
056        }
057        return identityService;
058    }
059
060    /**
061     *  Returns criteriaLookupService instance.
062     *  If it is not null returns existing instance else creates new instance.
063     * @return criteriaLookupService
064     */
065    protected CriteriaLookupService getCriteriaLookupService() {
066        if(criteriaLookupService == null) {
067            criteriaLookupService = GlobalResourceLoader.getService("criteriaLookupService");
068        }
069        return criteriaLookupService;
070    }
071
072    /**
073     * This method returns the object of OleLicesneRequestService
074     * @return  oleLicenseRequestService
075     */
076    public OleLicenseRequestService getOleLicenseRequestservice() {
077        if(oleLicenseRequestService == null ) {
078            oleLicenseRequestService = GlobalResourceLoader.getService("oleLicenseRequestService");
079        }
080        return oleLicenseRequestService;
081    }
082    /**                                                                                                                N
083     *  This method creates license request for maintenanceDocument using  document number and  licenseRequest.
084     * @param documentnumber
085     * @param itemUUIDs
086     */
087    @Override
088    public OleLicenseRequestBo createLicenseRequest(String documentnumber, String itemUUIDs) {
089        try{
090            String user = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OleLicenseRequest.LICENSE_DOCUMENT_INITIATOR);
091            GlobalVariables.setUserSession(new UserSession(user));
092            DocumentService documentService= GlobalResourceLoader.getService(OLEConstants.DOCUMENT_HEADER_SERVICE);
093
094            MaintenanceDocument licenseDoc = (MaintenanceDocument) documentService.getNewDocument(OLEConstants.OleLicenseRequest.LICENSE_REQUEST_DOC_TYPE);
095            OleLicenseRequestBo oleLicenseRequestBo = (OleLicenseRequestBo) licenseDoc.getDocumentDataObject();
096            oleLicenseRequestBo.setLocationId(OLEConstants.OleLicenseRequest.LICENSE_INITIAL_LOCATON);
097            oleLicenseRequestBo.setLicenseRequestWorkflowTypeCode(OLEConstants.OleLicenseRequest.LICENSE_INITIAL_WORKFLOW);
098            OleEventLogBo eventLog = new OleEventLogBo();
099            eventLog.setEventType("system");
100            eventLog.setCreatedBy(user);
101            eventLog.setEventDescription(OLEConstants.OleLicenseRequest.LICENSE_REQ_INTIAL_EVENT_LOG);
102            eventLog.setCurrentTimeStamp();
103            oleLicenseRequestBo.getEventLogs().add(eventLog);
104            oleLicenseRequestBo.seteResourceDocNumber(documentnumber);
105            oleLicenseRequestBo.setLicenseRequestStatusCode(
106                    ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OleLicenseRequest.LICENSE_REQ_DOCUMENT_STATUS));
107            if(licenseDoc.getDocumentNumber() != null) {
108                oleLicenseRequestBo.setDocumentNumber(licenseDoc.getDocumentNumber());
109                oleLicenseRequestBo.setLicenseDocumentNumber(licenseDoc.getDocumentNumber());
110            }
111
112            List<OleLicenseRequestItemTitle> oleLicenseRequestItemTitles= new ArrayList<OleLicenseRequestItemTitle>();
113            OleLicenseRequestItemTitle oleLicenseRequestItemTitle;
114            String[] bibUUIDs;
115            if (itemUUIDs != null ) {
116                bibUUIDs=itemUUIDs.split(",");
117                for(int i=0;i<bibUUIDs.length;i++){
118                    oleLicenseRequestItemTitle=new OleLicenseRequestItemTitle();
119                    oleLicenseRequestItemTitle.setItemUUID(bibUUIDs[i]);
120                    oleLicenseRequestItemTitles.add(oleLicenseRequestItemTitle);
121                }
122            }
123
124            oleLicenseRequestBo.setOleLicenseRequestItemTitles(oleLicenseRequestItemTitles);
125            Date now = CoreApiServiceLocator.getDateTimeService().getCurrentSqlDate();
126            Map criteria = new HashMap();
127            criteria.put("code",
128                    ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.OleLicenseRequest.LICENSE_REQ_DOCUMENT_STATUS));
129            OleLicenseRequestStatus licenseRequestStatus = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleLicenseRequestStatus.class,
130                    criteria);
131            licenseDoc.getDocumentHeader().getWorkflowDocument().setApplicationDocumentStatus(licenseRequestStatus.getName());
132
133            licenseDoc.getDocumentHeader().setDocumentDescription(OLEConstants.OleLicenseRequest.LICENSE_DESC+"[date:"+now+"]");
134            licenseDoc.getNewMaintainableObject().setDataObject(oleLicenseRequestBo);
135            MaintenanceDocument createdLicenseRequest = (MaintenanceDocument)documentService.saveDocument(licenseDoc);
136            DocumentRouteHeaderValue documentHeader= DocumentRouteHeaderValue.from(licenseDoc.getDocumentHeader().getWorkflowDocument().getDocument());
137            oleLicenseRequestBo.setDocumentRouteHeaderValue(documentHeader);
138            return oleLicenseRequestBo;
139
140        }catch(Exception e){
141            LOG.error("Exception while creating license request"+e.getMessage());
142            throw new RuntimeException(e);
143        }
144    }
145
146    /**
147     * This method returns the licenseRequestDocument number based on the requisitionDocument number
148     * @param reqDocNum
149     * @return  licenseRequestDocNum
150     */
151    /*@Override
152    public String getLicenseRequestDocNumber(String reqDocNum) {
153        String licenseRequestDocNum = "";
154        if(reqDocNum != null && !reqDocNum.isEmpty()) {
155            licenseRequestDocNum = getOleLicenseRequestservice().getLicenseRequestByRequisitionDocNum(reqDocNum);
156        }
157        return licenseRequestDocNum;
158    }
159*/
160    /**
161     *  This method returns URL based on the property  oleRequisitionWebService.url
162     * @return   url
163     */
164    public String getURL() {
165        String url = ConfigContext.getCurrentContextConfig().getProperty("oleRequisitionWebService.url");
166        return url;
167    }
168
169
170}