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.EntityLongRangeEdit;
021import com.echothree.control.user.core.common.form.EditEntityLongRangeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityLongRangeResult;
024import com.echothree.control.user.core.common.spec.EntityLongRangeSpec;
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.EntityLongRange;
031import com.echothree.model.data.core.server.entity.EntityLongRangeDescription;
032import com.echothree.model.data.core.server.entity.EntityLongRangeDetail;
033import com.echothree.model.data.core.server.entity.EntityType;
034import com.echothree.model.data.core.server.value.EntityLongRangeDescriptionValue;
035import com.echothree.model.data.core.server.value.EntityLongRangeDetailValue;
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 EditEntityLongRangeCommand
050        extends BaseAbstractEditCommand<EntityLongRangeSpec, EntityLongRangeEdit, EditEntityLongRangeResult, EntityLongRange, EntityLongRange> {
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.EntityLongRange.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("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null)
069                ));
070        
071        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("MinimumLongValue", FieldType.SIGNED_LONG, false, null, null),
074                new FieldDefinition("MaximumLongValue", FieldType.SIGNED_LONG, 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 EditEntityLongRangeCommand */
082    public EditEntityLongRangeCommand(UserVisitPK userVisitPK, EditEntityLongRangeForm form) {
083        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
084    }
085    
086    @Override
087   public EditEntityLongRangeResult getResult() {
088        return CoreResultFactory.getEditEntityLongRangeResult();
089    }
090
091    @Override
092    public EntityLongRangeEdit getEdit() {
093        return CoreEditFactory.getEntityLongRangeEdit();
094    }
095
096    EntityAttribute entityAttribute = null;
097    
098    @Override
099    public EntityLongRange getEntity(EditEntityLongRangeResult result) {
100        var coreControl = getCoreControl();
101        EntityLongRange entityLongRange = 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 entityLongRangeName = spec.getEntityLongRangeName();
116
117                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
118                        entityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName);
119                    } else { // EditMode.UPDATE
120                        entityLongRange = coreControl.getEntityLongRangeByNameForUpdate(entityAttribute, entityLongRangeName);
121                    }
122
123                    if(entityLongRange == null) {
124                        addExecutionError(ExecutionErrors.UnknownEntityLongRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityLongRangeName);
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 entityLongRange;
137    }
138
139    @Override
140    public EntityLongRange getLockEntity(EntityLongRange entityLongRange) {
141        return entityLongRange;
142    }
143
144    @Override
145    public void fillInResult(EditEntityLongRangeResult result, EntityLongRange entityLongRange) {
146        var coreControl = getCoreControl();
147
148        result.setEntityLongRange(coreControl.getEntityLongRangeTransfer(getUserVisit(), entityLongRange, null));
149    }
150
151    @Override
152    public void doLock(EntityLongRangeEdit edit, EntityLongRange entityLongRange) {
153        var coreControl = getCoreControl();
154        EntityLongRangeDescription entityLongRangeDescription = coreControl.getEntityLongRangeDescription(entityLongRange, getPreferredLanguage());
155        EntityLongRangeDetail entityLongRangeDetail = entityLongRange.getLastDetail();
156        Long minimumLongValue = entityLongRangeDetail.getMinimumLongValue();
157        Long maximumLongValue = entityLongRangeDetail.getMaximumLongValue();
158
159        edit.setEntityLongRangeName(entityLongRangeDetail.getEntityLongRangeName());
160        edit.setMinimumLongValue(minimumLongValue == null ? null : minimumLongValue.toString());
161        edit.setMaximumLongValue(maximumLongValue == null ? null : maximumLongValue.toString());
162        edit.setIsDefault(entityLongRangeDetail.getIsDefault().toString());
163        edit.setSortOrder(entityLongRangeDetail.getSortOrder().toString());
164
165        if(entityLongRangeDescription != null) {
166            edit.setDescription(entityLongRangeDescription.getDescription());
167        }
168    }
169
170    @Override
171    public void canUpdate(EntityLongRange entityLongRange) {
172        String strMinimumLongValue = edit.getMinimumLongValue();
173        Long minimumLongValue = strMinimumLongValue == null ? null : Long.valueOf(strMinimumLongValue);
174        String strMaximumLongValue = edit.getMaximumLongValue();
175        Long maximumLongValue = strMaximumLongValue == null ? null : Long.valueOf(strMaximumLongValue);
176
177        if(minimumLongValue == null || maximumLongValue == null || maximumLongValue >= minimumLongValue) {
178            var coreControl = getCoreControl();
179            String entityLongRangeName = edit.getEntityLongRangeName();
180            EntityLongRange duplicateEntityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName);
181
182            if(duplicateEntityLongRange != null && !entityLongRange.equals(duplicateEntityLongRange)) {
183                addExecutionError(ExecutionErrors.DuplicateEntityLongRangeName.name(), entityLongRangeName);
184            }
185        } else {
186            addExecutionError(ExecutionErrors.MinimumValueGreaterThanMaximumValue.name(), strMinimumLongValue, strMaximumLongValue);
187        }
188    }
189
190    @Override
191    public void doUpdate(EntityLongRange entityLongRange) {
192        var coreControl = getCoreControl();
193        var partyPK = getPartyPK();
194        EntityLongRangeDetailValue entityLongRangeDetailValue = coreControl.getEntityLongRangeDetailValueForUpdate(entityLongRange);
195        EntityLongRangeDescription entityLongRangeDescription = coreControl.getEntityLongRangeDescriptionForUpdate(entityLongRange, getPreferredLanguage());
196        String strMinimumLongValue = edit.getMinimumLongValue();
197        String strMaximumLongValue = edit.getMaximumLongValue();
198        String description = edit.getDescription();
199
200        entityLongRangeDetailValue.setEntityLongRangeName(edit.getEntityLongRangeName());
201        entityLongRangeDetailValue.setMinimumLongValue(strMinimumLongValue == null ? null : Long.valueOf(strMinimumLongValue));
202        entityLongRangeDetailValue.setMaximumLongValue(strMaximumLongValue == null ? null : Long.valueOf(strMaximumLongValue));
203        entityLongRangeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
204        entityLongRangeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
205
206        coreControl.updateEntityLongRangeFromValue(entityLongRangeDetailValue, partyPK);
207
208        if(entityLongRangeDescription == null && description != null) {
209            coreControl.createEntityLongRangeDescription(entityLongRange, getPreferredLanguage(), description, partyPK);
210        } else {
211            if(entityLongRangeDescription != null && description == null) {
212                coreControl.deleteEntityLongRangeDescription(entityLongRangeDescription, partyPK);
213            } else {
214                if(entityLongRangeDescription != null && description != null) {
215                    EntityLongRangeDescriptionValue entityLongRangeDescriptionValue = coreControl.getEntityLongRangeDescriptionValue(entityLongRangeDescription);
216
217                    entityLongRangeDescriptionValue.setDescription(description);
218                    coreControl.updateEntityLongRangeDescriptionFromValue(entityLongRangeDescriptionValue, partyPK);
219                }
220            }
221        }
222    }
223    
224}