001/*
002 * Copyright 2011 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.ole.select.document.validation.impl;
017
018import org.kuali.ole.select.OleSelectConstant;
019import org.kuali.ole.select.businessobject.OleDefaultTableColumn;
020import org.kuali.ole.select.constants.OleSelectPropertyConstants;
021import org.kuali.ole.sys.context.SpringContext;
022import org.kuali.rice.kns.document.MaintenanceDocument;
023import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
024import org.kuali.rice.krad.service.BusinessObjectService;
025
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030public class OleDefaultTableColumnRule extends MaintenanceDocumentRuleBase {
031
032    @Override
033    protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
034
035        boolean valid = processValidation(document);
036        return valid & super.processCustomRouteDocumentBusinessRules(document);
037    }
038
039    private boolean processValidation(MaintenanceDocument document) {
040        boolean valid = true;
041        valid &= processDocumentTypeValidation(document);
042        return valid;
043    }
044
045    private boolean processDocumentTypeValidation(MaintenanceDocument document) {
046        boolean valid = true;
047        OleDefaultTableColumn oleDefaultTableColumn = (OleDefaultTableColumn) document.getNewMaintainableObject().getBusinessObject();
048        String documentTypeId = oleDefaultTableColumn.getDocumentTypeId();
049        Map<String, Object> documentTypes = new HashMap<String, Object>();
050        documentTypes.put("documentTypeId", documentTypeId);
051        List<OleDefaultTableColumn> documentTypeList = (List) SpringContext.getBean(BusinessObjectService.class).findMatching(OleDefaultTableColumn.class, documentTypes);
052        if (documentTypeList.size() > 0) {
053            for (int i = 0; i < documentTypeList.size(); i++) {
054                if (documentTypeList.get(i).getDocumentTypeId().toString().equalsIgnoreCase(oleDefaultTableColumn.getDocumentTypeId().toString()) && documentTypeList.get(i).getDocumentColumn().equalsIgnoreCase(oleDefaultTableColumn.getDocumentColumn())) {
055                    valid = false;
056                    putFieldError(OleSelectConstant.DEFAULT_TABLE_COLUMN_DOCUMENTTYPE, OleSelectPropertyConstants.ERROR_DOCUMENTTYPE_EXIST);
057                }
058            }
059        }
060        return valid;
061    }
062}