001package org.kuali.ole.deliver.maintenance;
002
003import org.apache.commons.collections.CollectionUtils;
004import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
005import org.kuali.ole.OLEConstants;
006import org.kuali.ole.OLEPropertyConstants;
007import org.kuali.ole.deliver.bo.*;
008import org.kuali.ole.deliver.form.OlePatronMaintenanceDocumentForm;
009import org.kuali.ole.deliver.processor.LoanProcessor;
010import org.kuali.ole.deliver.service.OleDeliverRequestDocumentHelperServiceImpl;
011import org.kuali.ole.krad.OleComponentUtils;
012import org.kuali.ole.service.OlePatronService;
013import org.kuali.ole.service.OlePatronServiceImpl;
014import org.kuali.rice.core.api.config.property.ConfigContext;
015import org.kuali.rice.kim.api.KimConstants;
016import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
017import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
018import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
019import org.kuali.rice.kim.impl.identity.entity.EntityBo;
020import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
021import org.kuali.rice.kim.impl.identity.name.EntityNameTypeBo;
022import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
023import org.kuali.rice.krad.maintenance.MaintainableImpl;
024import org.kuali.rice.krad.maintenance.MaintenanceDocument;
025import org.kuali.rice.krad.service.KRADServiceLocator;
026import org.kuali.rice.krad.uif.util.ProcessLogger;
027
028import java.util.*;
029
030/**
031 * OlePatronMaintenanceImpl performs save ,edit,copy  and create new operation for OlePatron.
032 */
033public class OlePatronMaintenanceImpl extends MaintainableImpl {
034    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronMaintenanceImpl.class);
035    OlePatronService olePatronService = new OlePatronServiceImpl();
036
037    String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE);
038    /**
039     * This method populate the patron object used for edit and copy
040     *
041     * @param document
042     * @param dataObjectKeys
043     * @return olePatron(Object)
044     */
045    @Override
046    public Object retrieveObjectForEditOrCopy(MaintenanceDocument document, Map<String, String> dataObjectKeys) {
047        LOG.debug("Inside retrieveObjectForEditOrCopy()");
048        Object dataObject = null;
049        OlePatronDocument olePatron = new OlePatronDocument();
050        OleEntityAddressBo entityAddressBo = null;
051        OleAddressBo oleAddressBo = new OleAddressBo();
052        List<OleEntityAddressBo> oleEntityAddressList = new ArrayList<OleEntityAddressBo>();
053        try {
054            dataObject = getLookupService().findObjectBySearch(getDataObjectClass(), dataObjectKeys);
055            olePatron = (OlePatronDocument) dataObject;
056            EntityBo entity = olePatron.getEntity();
057            if (entity.getNames().size() > 0) {
058                EntityNameBo name = entity.getNames().get(0);
059                olePatron.setName(name);
060            }
061            if (!entity.getEntityTypeContactInfos().isEmpty()) {
062                List<EntityAddressBo> entityAddressList = entity.getEntityTypeContactInfos().get(0).getAddresses();
063                for (EntityAddressBo entityAdd : entityAddressList) {
064                    entityAddressBo = new OleEntityAddressBo();
065                    Map addMap = new HashMap();
066                    addMap.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityAdd.getId());
067                    oleAddressBo = getBusinessObjectService().findByPrimaryKey(OleAddressBo.class, addMap);
068                    entityAddressBo.setOleAddressBo(oleAddressBo);
069                    entityAddressBo.setEntityAddressBo(entityAdd);
070                    oleEntityAddressList.add(entityAddressBo);
071                }
072                olePatron.setOleEntityAddressBo(oleEntityAddressList);
073                olePatron.setPhones(entity.getEntityTypeContactInfos().get(0).getPhoneNumbers());
074                olePatron.setEmails(entity.getEntityTypeContactInfos().get(0).getEmailAddresses());
075            }
076            
077            olePatron.setEmployments(entity.getEmploymentInformation());
078            List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
079            olePatron.setPatronAffiliations(getPatronAffiliationFromEntity(entity.getAffiliations(), entity.getEmploymentInformation()));
080
081            if (olePatron.getOlePatronId() != null) {
082                LoanProcessor loanProcessor = new LoanProcessor();
083               /* OleDeliverRequestDocumentHelperServiceImpl requestService = new OleDeliverRequestDocumentHelperServiceImpl();
084                List<OleDeliverRequestBo> oleDeliverRequestBoList = olePatron.getOleDeliverRequestBos();
085                if (oleDeliverRequestBoList.size() > 0) {
086                    for (int i = 0; i < oleDeliverRequestBoList.size(); i++) {
087                        OleItemSearch oleItemSearch = requestService.getItemDetailsForPatron(oleDeliverRequestBoList.get(i).getItemUuid());
088                        if (oleItemSearch != null && oleItemSearch.getItemBarCode() != null) {
089                            oleDeliverRequestBoList.get(i).setTitle(oleItemSearch.getTitle());
090                            oleDeliverRequestBoList.get(i).setCallNumber(oleItemSearch.getCallNumber());
091                        }
092                    }
093                }
094                try {
095                    olePatron.setOleLoanDocuments(loanProcessor.getPatronLoanedItemBySolr(olePatron.getOlePatronId()));
096                    olePatron.setOleTemporaryCirculationHistoryRecords(loanProcessor.getPatronTemporaryCirculationHistoryRecords(olePatron.getOlePatronId()));
097                    olePatron.setOleDeliverRequestBos(loanProcessor.getPatronRequestRecords(olePatron.getOlePatronId()));
098                } catch (Exception e) {
099                    LOG.error("Exception", e);
100                }*/
101                if (olePatron.getOleLoanDocuments().size() > 0) {
102                    olePatron.setLoanFlag(true);
103                }
104                if (olePatron.getOleTemporaryCirculationHistoryRecords().size() > 0) {
105                    olePatron.setTempCircHistoryFlag(true);
106                }
107                if (olePatron.getOleDeliverRequestBos().size() > 0) {
108                    olePatron.setRequestFlag(true);
109                }
110                OlePatronDocument olePatronDocument;
111                OleProxyPatronDocument proxyPatronDocument = null;
112                List<OleProxyPatronDocument> proxyPatronDocuments = new ArrayList<OleProxyPatronDocument>();
113                Map proxyMap = new HashMap();
114                proxyMap.put(OLEConstants.OlePatron.PROXY_PATRON_ID, olePatron.getOlePatronId());
115                List<OleProxyPatronDocument> oleProxyPatronDocumentList = (List<OleProxyPatronDocument>) getBusinessObjectService().findMatching(OleProxyPatronDocument.class, proxyMap);
116                if (oleProxyPatronDocumentList.size() > 0) {
117                    for (OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocumentList) {
118                        proxyMap.remove(OLEConstants.OlePatron.PROXY_PATRON_ID);
119                        proxyMap.put(OLEConstants.OlePatron.PATRON_ID, oleProxyPatronDocument.getOlePatronId());
120                        List<OlePatronDocument> olePatronDocumentList = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, proxyMap);
121                        if (olePatronDocumentList.size() > 0) {
122                            olePatronDocument = olePatronDocumentList.get(0);
123                            proxyPatronDocument = new OleProxyPatronDocument();
124                           // if (olePatronDocument.isActiveIndicator()) {
125                                proxyPatronDocument.setOlePatronId(olePatronDocument.getOlePatronId());
126                                proxyPatronDocument.setProxyForPatronFirstName(olePatronDocument.getEntity().getNames().get(0).getFirstName());
127                                proxyPatronDocument.setProxyForPatronLastName(olePatronDocument.getEntity().getNames().get(0).getLastName());
128                                proxyPatronDocument.setProxyPatronActivationDate(oleProxyPatronDocument.getProxyPatronActivationDate());
129                                proxyPatronDocument.setProxyPatronExpirationDate(oleProxyPatronDocument.getProxyPatronExpirationDate());
130
131                         //   }
132                            proxyPatronDocuments.add(proxyPatronDocument);
133                        }
134                    }
135                    olePatron.setOleProxyPatronDocumentList(proxyPatronDocuments);
136                }
137
138                if(olePatron.getOleLoanDocuments()!=null){
139                    olePatron.setLoanCount(olePatron.getOleLoanDocuments().size());
140                }
141                if(olePatron.getOleTemporaryCirculationHistoryRecords()!=null){
142                    olePatron.setTempCirculationHistoryCount(olePatron.getOleTemporaryCirculationHistoryRecords().size());
143                }
144                if(olePatron.getOleDeliverRequestBos()!=null){
145                    olePatron.setRequestedItemRecordsCount(olePatron.getOleDeliverRequestBos().size());
146                }
147            }
148        } catch (ClassNotPersistenceCapableException ex) {
149            if (!document.getOldMaintainableObject().isExternalBusinessObject()) {
150                LOG.error("Data Object Class: "
151                        + getDataObjectClass()
152                        + " is not persistable and is not externalizable - configuration error", ex);
153                throw new RuntimeException("Data Object Class: "
154                        + getDataObjectClass()
155                        + " is not persistable and is not externalizable - configuration error");
156            }
157        }
158        return olePatron;
159    }
160
161    /**
162     * This method will set the default value for the name object and also set the description when a new patron document is created
163     *
164     * @param document
165     * @param requestParameters
166     */
167    @Override
168    public void processAfterNew(MaintenanceDocument document,
169                                Map<String, String[]> requestParameters) {
170        LOG.debug("Inside processAfterNew()");
171        EntityNameTypeBo nameTypeBo = (EntityNameTypeBo) KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EntityNameTypeBo.class, OLEConstants.OlePatron.ENTITY_NM_TYP_CD);
172        OlePatronDocument patronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
173        patronDocument.setActiveIndicator(true);
174        patronDocument.getName().setNameCode(OLEConstants.OlePatron.ENTITY_NM_TYP_CD);
175        patronDocument.getName().setActive(true);
176        patronDocument.getName().setDefaultValue(true);
177        patronDocument.getName().setNameType(nameTypeBo);
178        document.getNewMaintainableObject().setDataObject(patronDocument);
179        super.processAfterNew(document, requestParameters);
180        document.getDocumentHeader().setDocumentDescription("New Patron Document");
181
182    }
183
184    /**
185     * This method will set the description when copy is selected
186     *
187     * @param document
188     * @param requestParameters
189     */
190    @Override
191    public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> requestParameters) {
192        LOG.debug("Inside processAfterCopy()");
193        super.processAfterCopy(document, requestParameters);
194        document.getDocumentHeader().setDocumentDescription("Copied Patron Document");
195    }
196
197    /**
198     * This method will set the description for edit operation
199     *
200     * @param document
201     * @param requestParameters
202     */
203    @Override
204    public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
205        LOG.debug("Inside processAfterEdit()");
206        super.processAfterEdit(document, requestParameters);
207        document.getDocumentHeader().setDocumentDescription("Edited Patron Document");
208    }
209
210    private List<OlePatronAffiliation> getPatronAffiliationFromEntity(List<EntityAffiliationBo> affiliations,
211                                                                      List<EntityEmploymentBo> employeeDetails) {
212        List<OlePatronAffiliation> patronAffiliations = new ArrayList<OlePatronAffiliation>();
213        for (EntityAffiliationBo entityAffiliationBo : affiliations) {
214            OlePatronAffiliation patronAffiliation = new OlePatronAffiliation(entityAffiliationBo);
215            List<EntityEmploymentBo> employmentBos = new ArrayList<EntityEmploymentBo>();
216            for (EntityEmploymentBo entityEmploymentBo : employeeDetails) {
217                if (patronAffiliation.getEntityAffiliationId().equalsIgnoreCase(entityEmploymentBo.getEntityAffiliationId())) {
218                    employmentBos.add(entityEmploymentBo);
219                }
220                patronAffiliation.setEmployments(employmentBos);
221            }
222            patronAffiliations.add(patronAffiliation);
223        }
224        return patronAffiliations;
225    }
226
227    public String getTempCircRecords(String olePatronId) {
228        String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/temporaryCirculationRecord?viewId=OleTemporaryCirculationHistoryRecordView&amp;methodToCall=viewTempCircRecords&amp;patronId=" + olePatronId;
229        return url;
230    }
231
232    public String getLoanedRecords(String olePatronId) {
233        String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronLoanedRecord?viewId=OlePatronLoanedRecordView&amp;methodToCall=viewLoanedRecords&amp;patronId=" + olePatronId;
234        return url;
235    }
236
237    public String getRequestedRecords(String olePatronId) {
238        String url = baseUrl + "/portal.do?channelTitle=Patron&channelUrl=" + baseUrl + "/ole-kr-krad/patronRequestedRecord?viewId=OlePatronRequestedRecordView&amp;methodToCall=viewRequestedRecords&amp;patronId=" + olePatronId;
239        return url;
240    }
241
242    public String getCountOfPendingRequests(String itemId) {
243        Map itemMap = new HashMap();
244        itemMap.put(OLEConstants.OleDeliverRequest.ITEM_ID, itemId);
245        List<OleDeliverRequestBo> oleDeliverRequestBoList = (List<OleDeliverRequestBo>) getBusinessObjectService().findMatching(OleDeliverRequestBo.class, itemMap);
246        if (oleDeliverRequestBoList != null && oleDeliverRequestBoList.size() > 0) {
247
248            return "View all requests";
249        } else
250            return " ";
251    }
252}