/*
 * Copyright (c) 2005-2010 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.framework.components.fieldConfiguration.business;

import org.mifos.application.servicefacade.ApplicationContextProvider;
import org.mifos.application.util.helpers.EntityType;
import org.mifos.framework.business.EntityMaster;
import org.mifos.framework.components.fieldConfiguration.util.helpers.FieldConfigurationConstant;
import org.mifos.framework.exceptions.PersistenceException;
import org.mifos.framework.persistence.LegacyGenericDao;

public class FieldConfigurationEntity {

    private Integer fieldConfigId;
    private String fieldName;
    private EntityMaster entityMaster;
    private Short mandatoryFlag;
    private Short hiddenFlag;
    private FieldConfigurationEntity parentFieldConfig;

    public FieldConfigurationEntity() {
    }

    public Integer getFieldConfigId() {
        return fieldConfigId;
    }

    @SuppressWarnings("unused")
    // see .hbm.xml file
    private void setFieldConfigId(Integer fieldConfigId) {
        this.fieldConfigId = fieldConfigId;
    }

    @SuppressWarnings("unused")
    // see .hbm.xml file
    private void setHiddenFlag(Short hiddenFlag) {
        this.hiddenFlag = hiddenFlag;
    }

    public Short getHiddenFlag() {
        return hiddenFlag;
    }

    public boolean isHidden() {
        // TODO: what about null and other values besides 0 or 1?
        // should those be an exception (can't happen) or what?
        return hiddenFlag == FieldConfigurationConstant.YES;
    }

    public Short getMandatoryFlag() {
        return mandatoryFlag;
    }

    public boolean isMandatory() {
        // TODO: what about null and other values besides 0 or 1?
        // should those be an exception (can't happen) or what?
        return FieldConfigurationConstant.YES.equals(mandatoryFlag);
    }

    @SuppressWarnings("unused")
    // see .hbm.xml file
    private void setMandatoryFlag(Short mandatoryFlag) {
        this.mandatoryFlag = mandatoryFlag;
    }

    /**
     * Most callers instead want {@link #getEntityType()}.
     */
    EntityMaster getEntityMaster() {
        return entityMaster;
    }

    public EntityType getEntityType() {
        return EntityType.fromInt(entityMaster.getId());
    }

    @SuppressWarnings("unused")
    // see .hbm.xml file
    private void setEntityMaster(EntityMaster entityMaster) {
        this.entityMaster = entityMaster;
    }

    public String getFieldName() {
        return fieldName;
    }

    @SuppressWarnings("unused")
    // see .hbm.xml file
    private void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public FieldConfigurationEntity getParentFieldConfig() {
        return parentFieldConfig;
    }

    public void setParentFieldConfig(FieldConfigurationEntity parentFieldConfig) {
        this.parentFieldConfig = parentFieldConfig;
    }

    public String getLabel() {
        return this.getEntityMaster().getEntityType() + "." + this.getFieldName();
    }

    public void update(Short mandatoryFlag, Short hiddenFlag) throws PersistenceException {
        this.mandatoryFlag = mandatoryFlag;
        this.hiddenFlag = hiddenFlag;
        ApplicationContextProvider.getBean(LegacyGenericDao.class).createOrUpdate(this);
    }

}

@@ -825,16 +825,81 @@
 iness;%0A%0A
+import org.mifos.application.master.persistence.LegacyMasterDao;%0A
 import o
@@ -1226,65 +1226,8 @@
 ion;
-%0Aimport org.mifos.framework.persistence.LegacyGenericDao;
 %0A%0Apu
@@ -3943,15 +3943,14 @@
 gacy
-Generic
+Master
 Dao.
@@ -3988,7 +3988,5 @@
   %7D%0A
-%0A%7D%0A
+%7D

define mandatory fields not working due to reference to legacy abstract dao as bean.