/**
 * Copyright 2005-2012 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.kuali.rice.kew.document;

import org.kuali.rice.kew.doctype.bo.DocumentType;
import org.kuali.rice.kew.service.KEWServiceLocator;
import org.kuali.rice.krad.maintenance.MaintenanceDocument;
import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;

public class DocumentTypeMaintainableBusRule extends MaintenanceDocumentRuleBase {

    @Override
    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
        boolean result = super.processCustomRouteDocumentBusinessRules(document);

        result &= checkDoctypeName(document);

        return result;
    }

    /**
     * Checks if the doctype name already exist.
     *
     * @param document
     * @return false, if doctype name already exists otherwise true.
     */
    public boolean checkDoctypeName(MaintenanceDocument document) {
        boolean result = true;
        DocumentType bo = (DocumentType) document.getNewMaintainableObject().getDataObject();
        if (null != bo.getName()) {
            DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByName(bo.getName());
            if (null != documentType && document.isNew()) {
                result = false;
                putFieldError("name", "documenttype.name.duplicate");
            }
        } else {
            result = false;
            putFieldError("name", "documenttype.name.empty");
        }
        return result;
    }
}

@@ -672,16 +672,61 @@
 ent;%0D%0A%0D%0A
+import org.apache.commons.lang.StringUtils;%0D%0A
 import o
@@ -769,16 +769,16 @@
 tType;%0D%0A
-
 import o
@@ -1031,24 +1031,347 @@
 leBase %7B%0D%0A%0D%0A
+    @Override%0D%0A    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) %7B%0D%0A        boolean result = super.processCustomSaveDocumentBusinessRules(document);%0D%0A%0D%0A        result &= checkDoctypeName(document);%0D%0A        result &= checkDoctypeLabel(document);%0D%0A%0D%0A        return result;%0D%0A    %7D%0D%0A%0D%0A
     @Overrid
@@ -1591,32 +1591,80 @@
 Name(document);%0D
+%0A        result &= checkDoctypeLabel(document);%0D
 %0A%0D%0A        retur
@@ -2489,32 +2489,32 @@
 %22);%0D%0A        %7D%0D%0A
-
         return r
@@ -2527,12 +2527,578 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    /**%0D%0A     * Checks that the doctype label is specified.%0D%0A     *%0D%0A     * @param document%0D%0A     * @return false if doctype label is blank, otherwise true.%0D%0A     */%0D%0A    public boolean checkDoctypeLabel(MaintenanceDocument document) %7B%0D%0A        boolean isValid = true;%0D%0A        DocumentType bo = (DocumentType) document.getNewMaintainableObject().getDataObject();%0D%0A%0D%0A        isValid = !StringUtils.isBlank(bo.getLabel());%0D%0A%0D%0A        if (!isValid) %7B%0D%0A                putFieldError(%22label%22, %22documenttype.label.empty%22);%0D%0A        %7D%0D%0A%0D%0A        return isValid;%0D%0A    %7D%0D
 %0A%7D%0D%0A

document type label is not required on the document type document, but it's the only thing displayed many places and the kns blows up if it's not specified