001package org.kuali.ole.service;
002
003import org.apache.commons.lang.StringUtils;
004import org.kuali.ole.select.bo.OleCheckListBo;
005import org.kuali.rice.kew.api.exception.WorkflowException;
006import org.kuali.rice.krad.exception.DocumentTypeAuthorizationException;
007import org.kuali.rice.krad.maintenance.MaintenanceDocument;
008import org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl;
009import org.kuali.rice.krad.util.GlobalVariables;
010import org.kuali.rice.krad.util.KRADConstants;
011import org.kuali.rice.krad.util.ObjectUtils;
012
013import java.io.Serializable;
014import java.util.Map;
015
016/**
017 *OleCheckListMaintenanceDocumentServiceImpl performs maintenance document operation.
018 */
019public class OleCheckListMaintenanceDocumentServiceImpl extends MaintenanceDocumentServiceImpl implements OleCheckListMaintenanceDocumentService {
020
021    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleCheckListMaintenanceDocumentServiceImpl.class);
022
023    /**
024     *  This method creates new maintenance document instance for doc type
025     * @param objectClassName
026     * @param documentTypeName
027     * @param maintenanceAction
028     * @return MaintenanceDocument
029     */
030    @Override
031    public MaintenanceDocument setupNewMaintenanceDocument(String objectClassName, String documentTypeName,
032                                                           String maintenanceAction) {
033        if (StringUtils.isEmpty(objectClassName) && StringUtils.isEmpty(documentTypeName)) {
034            throw new IllegalArgumentException("Document type name or bo class not given!");
035        }
036
037        // get document type if not passed
038        if (StringUtils.isEmpty(documentTypeName)) {
039            try {
040                documentTypeName =
041                        getDocumentDictionaryService().getMaintenanceDocumentTypeName(Class.forName(objectClassName));
042            } catch (ClassNotFoundException e) {
043                throw new RuntimeException(e);
044            }
045
046            if (StringUtils.isEmpty(documentTypeName)) {
047                throw new RuntimeException(
048                        "documentTypeName is empty; does this Business Object have a maintenance document definition? " +
049                                objectClassName);
050            }
051        }
052
053        // check doc type allows new or copy if that action was requested
054        if (KRADConstants.MAINTENANCE_NEW_ACTION.equals(maintenanceAction) ||
055                KRADConstants.MAINTENANCE_COPY_ACTION.equals(maintenanceAction)) {
056            Class<?> boClass =
057                    getDocumentDictionaryService().getMaintenanceDataObjectClass(documentTypeName);
058            boolean allowsNewOrCopy = getDataObjectAuthorizationService()
059                    .canCreate(boClass, GlobalVariables.getUserSession().getPerson(), documentTypeName);
060            if (!allowsNewOrCopy) {
061                LOG.error("Document type " + documentTypeName + " does not allow new or copy actions.");
062                throw new DocumentTypeAuthorizationException(
063                        GlobalVariables.getUserSession().getPerson().getPrincipalId(), "newOrCopy", documentTypeName);
064            }
065        }
066
067        // get new document from service
068        try {
069            return (MaintenanceDocument) getDocumentService().getNewDocument(documentTypeName);
070        } catch (WorkflowException e) {
071            LOG.error("Cannot get new maintenance document instance for doc type: " + documentTypeName, e);
072            throw new RuntimeException("Cannot get new maintenance document instance for doc type: " + documentTypeName,
073                    e);
074        }
075    }
076
077    /**
078     * @see org.kuali.rice.krad.service.impl.MaintenanceDocumentServiceImpl#setupMaintenanceObject
079     */
080    /**
081     * This method creates maintenance object for delete operation using maintenanceAction.
082     * @param document
083     * @param maintenanceAction
084     * @param requestParameters
085     */
086    public void setupMaintenanceObjectForDelete(MaintenanceDocument document, String maintenanceAction,
087                                                Map<String, String[]> requestParameters) {
088        document.getNewMaintainableObject().setMaintenanceAction(maintenanceAction);
089        document.getOldMaintainableObject().setMaintenanceAction(maintenanceAction);
090
091        Object oldDataObject = retrieveObjectForMaintenance(document, requestParameters);
092        Object newDataObject = ObjectUtils.deepCopy((Serializable) oldDataObject);
093
094        document.getOldMaintainableObject().setDataObject(oldDataObject);
095        document.getNewMaintainableObject().setDataObject(newDataObject);
096    }
097
098    @Override
099    public void setupMaintenanceObject(MaintenanceDocument document, String maintenanceAction,
100                                       Map<String, String[]> requestParameters) {
101
102        super.setupMaintenanceObject(document,maintenanceAction,requestParameters);
103        // set object instance for editing
104
105        OleCheckListBo oldDataObject = (OleCheckListBo) document.getOldMaintainableObject().getDataObject();
106        OleCheckListBo newDataObject = (OleCheckListBo) ObjectUtils.deepCopy((Serializable) oldDataObject);
107        //newDataObject.setFileName(null);
108        document.getOldMaintainableObject().setDataObject(oldDataObject);
109        document.getNewMaintainableObject().setDataObject(newDataObject);
110    }
111}