package org.sigmah.shared.dto.element; /* * #%L * Sigmah * %% * Copyright (C) 2010 - 2016 URD * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ import com.extjs.gxt.ui.client.widget.Component; import com.extjs.gxt.ui.client.widget.form.Field; import com.extjs.gxt.ui.client.widget.form.Validator; import org.sigmah.client.i18n.I18N; import org.sigmah.client.ui.widget.form.StringField; import org.sigmah.shared.command.result.ValueResult; import org.sigmah.shared.computation.Computation; import org.sigmah.shared.computation.Computations; import org.sigmah.shared.computation.value.ComputedValue; import org.sigmah.shared.computation.value.ComputedValues; import org.sigmah.shared.dto.IsModel; import org.sigmah.shared.dto.ProjectModelDTO; import org.sigmah.shared.dto.element.event.ValueEvent; /** * Computation element DTO. * * @author Raphaƫl Calabro (raphael.calabro@netapsys.fr) * @since 2.1 */ public class ComputationElementDTO extends FlexibleElementDTO { /** * DTO corresponding entity name. */ public static final String ENTITY_NAME = "element.ComputationElement"; public static final String RULE = "rule"; public static final String MINIMUM_VALUE = "minimumValue"; public static final String MAXIMUM_VALUE = "maximumValue"; public static final String PROJECT_MODEL = "projectModel"; /** * {@inheritDoc} */ @Override protected Component getComponent(ValueResult valueResult, boolean enabled) { final StringField field = new StringField(); field.setFieldLabel(getLabel()); field.setValidator(new Validator() { @Override public String validate(Field<?> field, String value) { return validateValue(value); } }); preferredWidth = FlexibleElementDTO.NUMBER_FIELD_WIDTH; // Sets the value of the field. if (valueResult != null && valueResult.isValueDefined()) { field.setValue(valueResult.getValueObject()); } return field; } /** * {@inheritDoc} */ @Override public boolean isCorrectRequiredValue(ValueResult result) { final ComputedValue value = ComputedValues.from(result); return value.get() != null && value.matchesConstraints(this) == 0; } /** * {@inheritDoc} */ @Override public String getEntityName() { return ENTITY_NAME; } /** * Method in charge of firing value events. * * @param value * The raw value which is serialized to the server and saved to the data layer. */ public void fireValueEvent(final String value) { if (handlerManager == null) { init(); } handlerManager.fireEvent(new ValueEvent(ComputationElementDTO.this, value)); } /** * Creates a <code>Computation</code> from the current rule. * * @param model Model containing the flexible elements to use as variables. * @return A new <code>Computation</code> object. */ public Computation getComputationForModel(IsModel model) { return Computations.parse(getRule(), model.getAllElements()); } /** * Returns <code>true</code> if this element has a minimum or maximum value * constraint. * * @return <code>true</code> if this element has a minimum or maximum value * constraint, <code>false</code> otherwise. */ public boolean hasConstraints() { return getMinimumValueConstraint().get() != null || getMaximumValueConstraint().get() != null; } /** * Returns the minimum value constraint. * * @return minimum value constraint. */ public ComputedValue getMinimumValueConstraint() { return ComputedValues.from(getMinimumValue(), false); } /** * Returns the maximum value constraint. * * @return maximum value constraint. */ public ComputedValue getMaximumValueConstraint() { return ComputedValues.from(getMaximumValue(), false); } /** * Validate the given value. * * @param labelField View of this element. * @param value Value to validate. */ private String validateValue(final String value) { if (!hasConstraints()) { return null; } final ComputedValue computedValue = ComputedValues.from(value); switch (computedValue.matchesConstraints(this)) { case -1: return I18N.MESSAGES.flexibleElementComputationTooLow(value, getMinimumValue()); case 1: return I18N.MESSAGES.flexibleElementComputationTooHigh(value, getMaximumValue()); default: return null; } } // --------------------------------------------------------------------------------------------- // // GETTERS & SETTERS. // // --------------------------------------------------------------------------------------------- public String getRule() { return get(RULE); } public void setRule(String rule) { set(RULE, rule); } public String getMinimumValue() { return get(MINIMUM_VALUE); } public void setMinimumValue(String minimumValue) { set(MINIMUM_VALUE, minimumValue); } public String getMaximumValue() { return get(MAXIMUM_VALUE); } public void setMaximumValue(String maximumValue) { set(MAXIMUM_VALUE, maximumValue); } /** * Returns the project model containing this computation element. * This value is not available in normales cases. It is only filled when * this element is saved or loaded from the local database. * * @return <code>null</code> or the project model containing this element * if it was loaded from the local database. */ public ProjectModelDTO getProjectModel() { return get(PROJECT_MODEL); } /** * Defines the project model containing this element. * * @param projectModel * The project model containing this element. */ public void setProjectModel(ProjectModelDTO projectModel) { set(PROJECT_MODEL, projectModel); } }