001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.EntityAttribute;
029import com.echothree.model.data.core.server.entity.EntityLongRange;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.Arrays;
040import java.util.Collections;
041import java.util.List;
042import javax.enterprise.context.RequestScoped;
043
044@RequestScoped
045public class EditEntityLongRangeCommand
046        extends BaseAbstractEditCommand<EntityLongRangeSpec, EntityLongRangeEdit, EditEntityLongRangeResult, EntityLongRange, EntityLongRange> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
056                        new SecurityRoleDefinition(SecurityRoleGroups.EntityLongRange.name(), SecurityRoles.Edit.name())
057                        )))
058                )));
059        
060        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
063                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null)
065                ));
066        
067        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("MinimumLongValue", FieldType.SIGNED_LONG, false, null, null),
070                new FieldDefinition("MaximumLongValue", FieldType.SIGNED_LONG, false, null, null),
071                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                ));
075    }
076    
077    /** Creates a new instance of EditEntityLongRangeCommand */
078    public EditEntityLongRangeCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083   public EditEntityLongRangeResult getResult() {
084        return CoreResultFactory.getEditEntityLongRangeResult();
085    }
086
087    @Override
088    public EntityLongRangeEdit getEdit() {
089        return CoreEditFactory.getEntityLongRangeEdit();
090    }
091
092    EntityAttribute entityAttribute = null;
093    
094    @Override
095    public EntityLongRange getEntity(EditEntityLongRangeResult result) {
096        EntityLongRange entityLongRange = null;
097        var componentVendorName = spec.getComponentVendorName();
098        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
099
100        if(componentVendor != null) {
101            var entityTypeName = spec.getEntityTypeName();
102            var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
103
104            if(entityType != null) {
105                var entityAttributeName = spec.getEntityAttributeName();
106                
107                entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
108
109                if(entityAttribute != null) {
110                    var entityLongRangeName = spec.getEntityLongRangeName();
111
112                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
113                        entityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName);
114                    } else { // EditMode.UPDATE
115                        entityLongRange = coreControl.getEntityLongRangeByNameForUpdate(entityAttribute, entityLongRangeName);
116                    }
117
118                    if(entityLongRange == null) {
119                        addExecutionError(ExecutionErrors.UnknownEntityLongRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityLongRangeName);
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
129        }
130
131        return entityLongRange;
132    }
133
134    @Override
135    public EntityLongRange getLockEntity(EntityLongRange entityLongRange) {
136        return entityLongRange;
137    }
138
139    @Override
140    public void fillInResult(EditEntityLongRangeResult result, EntityLongRange entityLongRange) {
141
142        result.setEntityLongRange(coreControl.getEntityLongRangeTransfer(getUserVisit(), entityLongRange, null));
143    }
144
145    @Override
146    public void doLock(EntityLongRangeEdit edit, EntityLongRange entityLongRange) {
147        var entityLongRangeDescription = coreControl.getEntityLongRangeDescription(entityLongRange, getPreferredLanguage());
148        var entityLongRangeDetail = entityLongRange.getLastDetail();
149        var minimumLongValue = entityLongRangeDetail.getMinimumLongValue();
150        var maximumLongValue = entityLongRangeDetail.getMaximumLongValue();
151
152        edit.setEntityLongRangeName(entityLongRangeDetail.getEntityLongRangeName());
153        edit.setMinimumLongValue(minimumLongValue == null ? null : minimumLongValue.toString());
154        edit.setMaximumLongValue(maximumLongValue == null ? null : maximumLongValue.toString());
155        edit.setIsDefault(entityLongRangeDetail.getIsDefault().toString());
156        edit.setSortOrder(entityLongRangeDetail.getSortOrder().toString());
157
158        if(entityLongRangeDescription != null) {
159            edit.setDescription(entityLongRangeDescription.getDescription());
160        }
161    }
162
163    @Override
164    public void canUpdate(EntityLongRange entityLongRange) {
165        var strMinimumLongValue = edit.getMinimumLongValue();
166        var minimumLongValue = strMinimumLongValue == null ? null : Long.valueOf(strMinimumLongValue);
167        var strMaximumLongValue = edit.getMaximumLongValue();
168        var maximumLongValue = strMaximumLongValue == null ? null : Long.valueOf(strMaximumLongValue);
169
170        if(minimumLongValue == null || maximumLongValue == null || maximumLongValue >= minimumLongValue) {
171            var entityLongRangeName = edit.getEntityLongRangeName();
172            var duplicateEntityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName);
173
174            if(duplicateEntityLongRange != null && !entityLongRange.equals(duplicateEntityLongRange)) {
175                addExecutionError(ExecutionErrors.DuplicateEntityLongRangeName.name(), entityLongRangeName);
176            }
177        } else {
178            addExecutionError(ExecutionErrors.MinimumValueGreaterThanMaximumValue.name(), strMinimumLongValue, strMaximumLongValue);
179        }
180    }
181
182    @Override
183    public void doUpdate(EntityLongRange entityLongRange) {
184        var partyPK = getPartyPK();
185        var entityLongRangeDetailValue = coreControl.getEntityLongRangeDetailValueForUpdate(entityLongRange);
186        var entityLongRangeDescription = coreControl.getEntityLongRangeDescriptionForUpdate(entityLongRange, getPreferredLanguage());
187        var strMinimumLongValue = edit.getMinimumLongValue();
188        var strMaximumLongValue = edit.getMaximumLongValue();
189        var description = edit.getDescription();
190
191        entityLongRangeDetailValue.setEntityLongRangeName(edit.getEntityLongRangeName());
192        entityLongRangeDetailValue.setMinimumLongValue(strMinimumLongValue == null ? null : Long.valueOf(strMinimumLongValue));
193        entityLongRangeDetailValue.setMaximumLongValue(strMaximumLongValue == null ? null : Long.valueOf(strMaximumLongValue));
194        entityLongRangeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
195        entityLongRangeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
196
197        coreControl.updateEntityLongRangeFromValue(entityLongRangeDetailValue, partyPK);
198
199        if(entityLongRangeDescription == null && description != null) {
200            coreControl.createEntityLongRangeDescription(entityLongRange, getPreferredLanguage(), description, partyPK);
201        } else {
202            if(entityLongRangeDescription != null && description == null) {
203                coreControl.deleteEntityLongRangeDescription(entityLongRangeDescription, partyPK);
204            } else {
205                if(entityLongRangeDescription != null && description != null) {
206                    var entityLongRangeDescriptionValue = coreControl.getEntityLongRangeDescriptionValue(entityLongRangeDescription);
207
208                    entityLongRangeDescriptionValue.setDescription(description);
209                    coreControl.updateEntityLongRangeDescriptionFromValue(entityLongRangeDescriptionValue, partyPK);
210                }
211            }
212        }
213    }
214    
215}