001package org.kuali.ole.describe.rule;
002
003import org.kuali.ole.OLEConstants;
004import org.kuali.ole.describe.bo.OleShelvingScheme;
005import org.kuali.ole.describe.bo.ExternalDataSourceConfig;
006import org.kuali.rice.krad.maintenance.MaintenanceDocument;
007import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
008
009import java.util.HashMap;
010import java.util.List;
011import java.util.Map;
012
013/**
014 * Created with IntelliJ IDEA.
015 * User: PJ7789
016 * Date: 7/12/12
017 * Time: 6:40 PM
018 * To change this template use File | Settings | File Templates.
019 */
020public class OleExternalDatSourceConfigRule
021        extends MaintenanceDocumentRuleBase {
022
023
024    protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
025        boolean isValid = true;
026        ExternalDataSourceConfig externalDataSourceConfig = (ExternalDataSourceConfig) document
027                .getNewMaintainableObject().getDataObject();
028
029        isValid &= validateDuplicateDataSourceName(externalDataSourceConfig);
030        isValid &= validateEmptyDataSourceName(externalDataSourceConfig);
031        return isValid;
032    }
033
034    /**
035     * This method  validates duplicate external dataSource name, Id and return boolean value.
036     *
037     * @param externalDataSourceConfig
038     * @return boolean
039     */
040    private boolean validateDuplicateDataSourceName(ExternalDataSourceConfig externalDataSourceConfig) {
041
042        if (externalDataSourceConfig.getName() != null) {
043            Map<String, String> criteria = new HashMap<String, String>();
044            criteria.put(OLEConstants.OleExternalDataSourceConfig.DATA_SOURCE_NM, externalDataSourceConfig.getName());
045            List<ExternalDataSourceConfig> dataSourceNameInDatabase = (List<ExternalDataSourceConfig>) getBoService()
046                    .findMatching(ExternalDataSourceConfig.class, criteria);
047
048            if ((dataSourceNameInDatabase.size() > 0)) {
049                for (ExternalDataSourceConfig dataSourceObj : dataSourceNameInDatabase) {
050                    Integer dataSourceId = dataSourceObj.getId();
051                    if (null == externalDataSourceConfig.getId() || (externalDataSourceConfig.getId().intValue()
052                            != dataSourceId.intValue())) {
053                        this.putFieldError(OLEConstants.OleExternalDataSourceConfig.DATA_SOURCE_NAME,
054                                "error.duplicate.name");
055                        return false;
056                    }
057
058                }
059            }
060
061        }
062        return true;
063    }
064
065
066    /**
067     * This method  validates empty external Data Source name and return boolean value.
068     *
069     * @param externalDataSourceConfig
070     * @return boolean
071     */
072    private boolean validateEmptyDataSourceName(ExternalDataSourceConfig externalDataSourceConfig) {
073
074        if (externalDataSourceConfig.getName() == null || externalDataSourceConfig.getName().equalsIgnoreCase("")
075                || externalDataSourceConfig.getName().length() == 0) {
076            this.putFieldError(OLEConstants.OleExternalDataSourceConfig.DATA_SOURCE_NAME, "error.empty.name");
077            return false;
078        }
079        return true;
080    }
081}