001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.document;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kew.doctype.bo.DocumentType;
020import org.kuali.rice.kew.service.KEWServiceLocator;
021import org.kuali.rice.kns.document.MaintenanceDocument;
022import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
023
024public class DocumentTypeMaintainableBusRule extends MaintenanceDocumentRuleBase {
025
026    @Override
027    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
028        boolean result = super.processCustomSaveDocumentBusinessRules(document);
029
030        result &= checkDoctypeName(document);
031        result &= checkDoctypeLabel(document);
032
033        return result;
034    }
035
036    @Override
037    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
038        boolean result = super.processCustomRouteDocumentBusinessRules(document);
039
040        result &= checkDoctypeName(document);
041        result &= checkDoctypeLabel(document);
042
043        return result;
044    }
045
046    /**
047     * Checks if the doctype name already exist.
048     *
049     * @param document
050     * @return false, if doctype name already exists otherwise true.
051     */
052    public boolean checkDoctypeName(MaintenanceDocument document) {
053        boolean result = true;
054        DocumentType bo = (DocumentType) document.getNewMaintainableObject().getDataObject();
055        if (null != bo.getName()) {
056            DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(bo.getName());
057            if (null != documentType && document.isNew()) {
058                result = false;
059                putFieldError("name", "documenttype.name.duplicate");
060            }
061        } else {
062            result = false;
063        }
064        return result;
065    }
066
067    /**
068     * Checks that the doctype label is specified.
069     *
070     * @param document
071     * @return false if doctype label is blank, otherwise true.
072     */
073    public boolean checkDoctypeLabel(MaintenanceDocument document) {
074        boolean isValid = true;
075        DocumentType bo = (DocumentType) document.getNewMaintainableObject().getDataObject();
076
077        isValid = !StringUtils.isBlank(bo.getLabel());
078
079        return isValid;
080    }
081}