001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.core.server.command;
018
019import com.echothree.control.user.core.common.edit.CoreEditFactory;
020import com.echothree.control.user.core.common.edit.EntityIntegerRangeEdit;
021import com.echothree.control.user.core.common.form.EditEntityIntegerRangeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityIntegerRangeResult;
024import com.echothree.control.user.core.common.spec.EntityIntegerRangeSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.core.server.entity.ComponentVendor;
029import com.echothree.model.data.core.server.entity.EntityAttribute;
030import com.echothree.model.data.core.server.entity.EntityIntegerRange;
031import com.echothree.model.data.core.server.entity.EntityIntegerRangeDescription;
032import com.echothree.model.data.core.server.entity.EntityIntegerRangeDetail;
033import com.echothree.model.data.core.server.entity.EntityType;
034import com.echothree.model.data.core.server.value.EntityIntegerRangeDescriptionValue;
035import com.echothree.model.data.core.server.value.EntityIntegerRangeDetailValue;
036import com.echothree.model.data.user.common.pk.UserVisitPK;
037import com.echothree.util.common.message.ExecutionErrors;
038import com.echothree.util.common.validation.FieldDefinition;
039import com.echothree.util.common.validation.FieldType;
040import com.echothree.util.common.command.EditMode;
041import com.echothree.util.server.control.BaseAbstractEditCommand;
042import com.echothree.util.server.control.CommandSecurityDefinition;
043import com.echothree.util.server.control.PartyTypeDefinition;
044import com.echothree.util.server.control.SecurityRoleDefinition;
045import java.util.Arrays;
046import java.util.Collections;
047import java.util.List;
048
049public class EditEntityIntegerRangeCommand
050        extends BaseAbstractEditCommand<EntityIntegerRangeSpec, EntityIntegerRangeEdit, EditEntityIntegerRangeResult, EntityIntegerRange, EntityIntegerRange> {
051    
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
054    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
055    
056    static {
057        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
060                        new SecurityRoleDefinition(SecurityRoleGroups.EntityIntegerRange.name(), SecurityRoles.Edit.name())
061                        )))
062                )));
063        
064        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
067                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null)
069                ));
070        
071        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("MinimumIntegerValue", FieldType.SIGNED_INTEGER, false, null, null),
074                new FieldDefinition("MaximumIntegerValue", FieldType.SIGNED_INTEGER, false, null, null),
075                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
076                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
077                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
078                ));
079    }
080    
081    /** Creates a new instance of EditEntityIntegerRangeCommand */
082    public EditEntityIntegerRangeCommand(UserVisitPK userVisitPK, EditEntityIntegerRangeForm form) {
083        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087   public EditEntityIntegerRangeResult getResult() {
088        return CoreResultFactory.getEditEntityIntegerRangeResult();
089    }
090
091    @Override
092    public EntityIntegerRangeEdit getEdit() {
093        return CoreEditFactory.getEntityIntegerRangeEdit();
094    }
095
096    EntityAttribute entityAttribute = null;
097    
098    @Override
099    public EntityIntegerRange getEntity(EditEntityIntegerRangeResult result) {
100        var coreControl = getCoreControl();
101        EntityIntegerRange entityIntegerRange = null;
102        String componentVendorName = spec.getComponentVendorName();
103        ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
104
105        if(componentVendor != null) {
106            String entityTypeName = spec.getEntityTypeName();
107            EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
108
109            if(entityType != null) {
110                String entityAttributeName = spec.getEntityAttributeName();
111                
112                entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
113
114                if(entityAttribute != null) {
115                    String entityIntegerRangeName = spec.getEntityIntegerRangeName();
116
117                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
118                        entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName);
119                    } else { // EditMode.UPDATE
120                        entityIntegerRange = coreControl.getEntityIntegerRangeByNameForUpdate(entityAttribute, entityIntegerRangeName);
121                    }
122
123                    if(entityIntegerRange == null) {
124                        addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName);
125                    }
126                } else {
127                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
128                }
129            } else {
130                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
131            }
132        } else {
133            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
134        }
135
136        return entityIntegerRange;
137    }
138
139    @Override
140    public EntityIntegerRange getLockEntity(EntityIntegerRange entityIntegerRange) {
141        return entityIntegerRange;
142    }
143
144    @Override
145    public void fillInResult(EditEntityIntegerRangeResult result, EntityIntegerRange entityIntegerRange) {
146        var coreControl = getCoreControl();
147
148        result.setEntityIntegerRange(coreControl.getEntityIntegerRangeTransfer(getUserVisit(), entityIntegerRange, null));
149    }
150
151    @Override
152    public void doLock(EntityIntegerRangeEdit edit, EntityIntegerRange entityIntegerRange) {
153        var coreControl = getCoreControl();
154        EntityIntegerRangeDescription entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, getPreferredLanguage());
155        EntityIntegerRangeDetail entityIntegerRangeDetail = entityIntegerRange.getLastDetail();
156        Integer minimumIntegerValue = entityIntegerRangeDetail.getMinimumIntegerValue();
157        Integer maximumIntegerValue = entityIntegerRangeDetail.getMaximumIntegerValue();
158
159        edit.setEntityIntegerRangeName(entityIntegerRangeDetail.getEntityIntegerRangeName());
160        edit.setMinimumIntegerValue(minimumIntegerValue == null ? null : minimumIntegerValue.toString());
161        edit.setMaximumIntegerValue(maximumIntegerValue == null ? null : maximumIntegerValue.toString());
162        edit.setIsDefault(entityIntegerRangeDetail.getIsDefault().toString());
163        edit.setSortOrder(entityIntegerRangeDetail.getSortOrder().toString());
164
165        if(entityIntegerRangeDescription != null) {
166            edit.setDescription(entityIntegerRangeDescription.getDescription());
167        }
168    }
169
170    @Override
171    public void canUpdate(EntityIntegerRange entityIntegerRange) {
172        String strMinimumIntegerValue = edit.getMinimumIntegerValue();
173        Integer minimumIntegerValue = strMinimumIntegerValue == null ? null : Integer.valueOf(strMinimumIntegerValue);
174        String strMaximumIntegerValue = edit.getMaximumIntegerValue();
175        Integer maximumIntegerValue = strMaximumIntegerValue == null ? null : Integer.valueOf(strMaximumIntegerValue);
176
177        if(minimumIntegerValue == null || maximumIntegerValue == null || maximumIntegerValue >= minimumIntegerValue) {
178            var coreControl = getCoreControl();
179            String entityIntegerRangeName = edit.getEntityIntegerRangeName();
180            EntityIntegerRange duplicateEntityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName);
181
182            if(duplicateEntityIntegerRange != null && !entityIntegerRange.equals(duplicateEntityIntegerRange)) {
183                addExecutionError(ExecutionErrors.DuplicateEntityIntegerRangeName.name(), entityIntegerRangeName);
184            }
185        } else {
186            addExecutionError(ExecutionErrors.MinimumValueGreaterThanMaximumValue.name(), strMinimumIntegerValue, strMaximumIntegerValue);
187        }
188    }
189
190    @Override
191    public void doUpdate(EntityIntegerRange entityIntegerRange) {
192        var coreControl = getCoreControl();
193        var partyPK = getPartyPK();
194        EntityIntegerRangeDetailValue entityIntegerRangeDetailValue = coreControl.getEntityIntegerRangeDetailValueForUpdate(entityIntegerRange);
195        EntityIntegerRangeDescription entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescriptionForUpdate(entityIntegerRange, getPreferredLanguage());
196        String strMinimumIntegerValue = edit.getMinimumIntegerValue();
197        String strMaximumIntegerValue = edit.getMaximumIntegerValue();
198        String description = edit.getDescription();
199
200        entityIntegerRangeDetailValue.setEntityIntegerRangeName(edit.getEntityIntegerRangeName());
201        entityIntegerRangeDetailValue.setMinimumIntegerValue(strMinimumIntegerValue == null ? null : Integer.valueOf(strMinimumIntegerValue));
202        entityIntegerRangeDetailValue.setMaximumIntegerValue(strMaximumIntegerValue == null ? null : Integer.valueOf(strMaximumIntegerValue));
203        entityIntegerRangeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
204        entityIntegerRangeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
205
206        coreControl.updateEntityIntegerRangeFromValue(entityIntegerRangeDetailValue, partyPK);
207
208        if(entityIntegerRangeDescription == null && description != null) {
209            coreControl.createEntityIntegerRangeDescription(entityIntegerRange, getPreferredLanguage(), description, partyPK);
210        } else {
211            if(entityIntegerRangeDescription != null && description == null) {
212                coreControl.deleteEntityIntegerRangeDescription(entityIntegerRangeDescription, partyPK);
213            } else {
214                if(entityIntegerRangeDescription != null && description != null) {
215                    EntityIntegerRangeDescriptionValue entityIntegerRangeDescriptionValue = coreControl.getEntityIntegerRangeDescriptionValue(entityIntegerRangeDescription);
216
217                    entityIntegerRangeDescriptionValue.setDescription(description);
218                    coreControl.updateEntityIntegerRangeDescriptionFromValue(entityIntegerRangeDescriptionValue, partyPK);
219                }
220            }
221        }
222    }
223    
224}