001package org.kuali.ole.ncip.service.impl;
002
003import org.apache.log4j.Logger;
004import org.extensiblecatalog.ncip.v2.service.*;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.deliver.processor.LoanProcessor;
007import org.kuali.ole.ncip.bo.OLENCIPConstants;
008import org.kuali.ole.ncip.bo.OLERenewItem;
009import org.kuali.ole.ncip.converter.OLERenewItemConverter;
010import org.kuali.ole.ncip.service.OLECirculationService;
011import org.kuali.ole.ncip.service.OLERenewItemService;
012import org.kuali.ole.sys.context.SpringContext;
013import org.kuali.rice.core.api.config.property.ConfigContext;
014import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
015
016import java.math.BigDecimal;
017import java.util.ArrayList;
018import java.util.HashMap;
019import java.util.List;
020
021/**
022 * Created with IntelliJ IDEA.
023 * User: maheswarang
024 * Date: 2/19/14
025 * Time: 4:00 PM
026 * To change this template use File | Settings | File Templates.
027 */
028public class OLERenewItemServiceImpl implements OLERenewItemService {
029    private static final Logger LOG = Logger.getLogger(OLERequestItemServiceImpl.class);
030    private OLECirculationService oleCirculationService;
031    private OLECirculationHelperServiceImpl oleCirculationHelperService;
032    private OLERenewItemConverter oleRenewItemConverter = new OLERenewItemConverter();
033    private LoanProcessor loanProcessor;
034
035    public LoanProcessor getLoanProcessor() {
036        if (loanProcessor == null) {
037            loanProcessor = SpringContext.getBean(LoanProcessor.class);
038        }
039        return loanProcessor;
040    }
041
042    public OLECirculationService getOleCirculationService() {
043        if (null == oleCirculationService) {
044            oleCirculationService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_SERVICE);
045        }
046        return oleCirculationService;
047    }
048
049    public void setOleCirculationService(OLECirculationService oleCirculationService) {
050        this.oleCirculationService = oleCirculationService;
051    }
052
053    public OLECirculationHelperServiceImpl getOleCirculationHelperService() {
054        if (null == oleCirculationHelperService) {
055            oleCirculationHelperService = GlobalResourceLoader.getService(OLENCIPConstants.CIRCULATION_HELPER_SERVICE);
056        }
057        return oleCirculationHelperService;
058    }
059
060    public void setOleCirculationHelperService(OLECirculationHelperServiceImpl oleCirculationHelperService) {
061        this.oleCirculationHelperService = oleCirculationHelperService;
062    }
063
064    public OLERenewItemConverter getOleRenewItemConverter() {
065        return oleRenewItemConverter;
066    }
067
068    public void setOleRenewItemConverter(OLERenewItemConverter oleRenewItemConverter) {
069        this.oleRenewItemConverter = oleRenewItemConverter;
070    }
071
072    @Override
073    public RenewItemResponseData performService(RenewItemInitiationData renewItemInitiationData, ServiceContext serviceContext, RemoteServiceManager remoteServiceManager) throws ServiceException {
074
075        RenewItemResponseData renewItemResponseData = new RenewItemResponseData();
076        oleCirculationService = getOleCirculationService();
077        oleCirculationHelperService = getOleCirculationHelperService();
078        List<Problem> problems = new ArrayList<Problem>();
079        String responseString = null;
080        AgencyId agencyId = null;
081        Problem problem = new Problem();
082        ProblemType problemType = new ProblemType("");
083        if (renewItemInitiationData.getInitiationHeader() != null && renewItemInitiationData.getInitiationHeader().getFromAgencyId() != null && renewItemInitiationData.getInitiationHeader().getFromAgencyId().getAgencyId() != null && renewItemInitiationData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue() != null && !renewItemInitiationData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue().trim().isEmpty()) {
084            agencyId = new AgencyId(renewItemInitiationData.getInitiationHeader().getFromAgencyId().getAgencyId().getValue());
085        } else if (renewItemInitiationData.getInitiationHeader() != null && renewItemInitiationData.getInitiationHeader().getApplicationProfileType() != null && renewItemInitiationData.getInitiationHeader().getApplicationProfileType().getValue() != null && !renewItemInitiationData.getInitiationHeader().getApplicationProfileType().getValue().trim().isEmpty()) {
086            agencyId = new AgencyId(renewItemInitiationData.getInitiationHeader().getApplicationProfileType().getValue());
087        } else {
088            agencyId = new AgencyId(getLoanProcessor().getParameter(OLENCIPConstants.AGENCY_ID_PARAMETER));
089        }
090        HashMap<String, String> agencyPropertyMap = oleCirculationHelperService.getAgencyPropertyMap(agencyId.getValue());
091        if (agencyPropertyMap.size() > 0) {
092            String operatorId = agencyPropertyMap.get(OLENCIPConstants.OPERATOR_ID);
093            String patronId = renewItemInitiationData.getUserId().getUserIdentifierValue();
094            String itemBarcode = renewItemInitiationData.getItemId().getItemIdentifierValue();
095            LOG.info("Inside Renew Item Service . Patron Barcode :  " + patronId + " Operator Id : " +operatorId + "Item Barcode : " + itemBarcode);
096            String response = oleCirculationService.renewItem(patronId, operatorId, itemBarcode,false);
097            LOG.info(response);
098            OLERenewItem oleRenewItem = (OLERenewItem) oleRenewItemConverter.generateRenewItemObject(response);
099            if (oleRenewItem != null && oleRenewItem.getMessage().contains(ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.RENEW_SUCCESS))) {
100                renewItemResponseData.setItemId(renewItemInitiationData.getItemId());
101                renewItemResponseData.setUserId(renewItemInitiationData.getUserId());
102                renewItemResponseData.setDateDue(oleCirculationHelperService.getGregorianCalendarDate(oleRenewItem.getPastDueDate()));
103                renewItemResponseData.setDateForReturn(oleCirculationHelperService.getGregorianCalendarDate(oleRenewItem.getNewDueDate()));
104                renewItemResponseData.setRenewalCount(new BigDecimal(oleRenewItem.getRenewalCount()));
105            } else {
106                if (oleRenewItem != null) {
107                    problem.setProblemDetail(oleRenewItem.getMessage());
108                } else {
109                    problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.RENEW_FAIL));
110                }
111                problem.setProblemElement(OLENCIPConstants.ITEM);
112                problem.setProblemType(problemType);
113                problem.setProblemValue(itemBarcode);
114                problems.add(problem);
115                renewItemResponseData.setProblems(problems);
116            }
117        } else {
118            problem.setProblemDetail(ConfigContext.getCurrentContextConfig().getProperty(OLENCIPConstants.INVALID_AGENCY_ID));
119            problem.setProblemElement(OLENCIPConstants.AGENCY_ID);
120            problem.setProblemType(problemType);
121            problem.setProblemValue(agencyId.getValue());
122            problems.add(problem);
123            renewItemResponseData.setProblems(problems);
124        }
125
126        return renewItemResponseData;   //To change body of implemented methods use File | Settings | File Templates.
127    }
128
129
130}