001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.EntityIntegerRangeDescriptionEdit;
021import com.echothree.control.user.core.common.form.EditEntityIntegerRangeDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityIntegerRangeDescriptionResult;
024import com.echothree.control.user.core.common.spec.EntityIntegerRangeDescriptionSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.core.server.entity.EntityIntegerRange;
030import com.echothree.model.data.core.server.entity.EntityIntegerRangeDescription;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditEntityIntegerRangeDescriptionCommand
046        extends BaseAbstractEditCommand<EntityIntegerRangeDescriptionSpec, EntityIntegerRangeDescriptionEdit, EditEntityIntegerRangeDescriptionResult, EntityIntegerRangeDescription, EntityIntegerRange> {
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(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.EntityIntegerRange.name(), SecurityRoles.Description.name())
057                        ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
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("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
066                );
067        
068        EDIT_FIELD_DEFINITIONS = List.of(
069                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
070                );
071    }
072    
073    /** Creates a new instance of EditEntityIntegerRangeDescriptionCommand */
074    public EditEntityIntegerRangeDescriptionCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    public EditEntityIntegerRangeDescriptionResult getResult() {
080        return CoreResultFactory.getEditEntityIntegerRangeDescriptionResult();
081    }
082
083    @Override
084    public EntityIntegerRangeDescriptionEdit getEdit() {
085        return CoreEditFactory.getEntityIntegerRangeDescriptionEdit();
086    }
087
088    @Override
089    public EntityIntegerRangeDescription getEntity(EditEntityIntegerRangeDescriptionResult result) {
090        EntityIntegerRangeDescription entityIntegerRangeDescription = null;
091        var componentVendorName = spec.getComponentVendorName();
092        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
093
094        if(componentVendor != null) {
095            var entityTypeName = spec.getEntityTypeName();
096            var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
097
098            if(entityType != null) {
099                var entityAttributeName = spec.getEntityAttributeName();
100                var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
101
102                if(entityAttribute != null) {
103                    var entityIntegerRangeName = spec.getEntityIntegerRangeName();
104                    var entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName);
105
106                    if(entityIntegerRange != null) {
107                        var partyControl = Session.getModelController(PartyControl.class);
108                        var languageIsoName = spec.getLanguageIsoName();
109                        var language = partyControl.getLanguageByIsoName(languageIsoName);
110
111                        if(language != null) {
112                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
113                                entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, language);
114                            } else { // EditMode.UPDATE
115                                entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescriptionForUpdate(entityIntegerRange, language);
116                            }
117
118                            if(entityIntegerRangeDescription == null) {
119                                addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName,
120                                        entityIntegerRangeName, languageIsoName);
121                            }
122                        } else {
123                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
124                        }
125                    } else {
126                        addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName);
127                    }
128                } else {
129                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
130                }
131            } else {
132                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
133            }
134        } else {
135            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
136        }
137
138        return entityIntegerRangeDescription;
139    }
140
141    @Override
142    public EntityIntegerRange getLockEntity(EntityIntegerRangeDescription entityIntegerRangeDescription) {
143        return entityIntegerRangeDescription.getEntityIntegerRange();
144    }
145
146    @Override
147    public void fillInResult(EditEntityIntegerRangeDescriptionResult result, EntityIntegerRangeDescription entityIntegerRangeDescription) {
148
149        result.setEntityIntegerRangeDescription(coreControl.getEntityIntegerRangeDescriptionTransfer(getUserVisit(), entityIntegerRangeDescription, null));
150    }
151
152    @Override
153    public void doLock(EntityIntegerRangeDescriptionEdit edit, EntityIntegerRangeDescription entityIntegerRangeDescription) {
154        edit.setDescription(entityIntegerRangeDescription.getDescription());
155    }
156
157    @Override
158    public void doUpdate(EntityIntegerRangeDescription entityIntegerRangeDescription) {
159        var entityIntegerRangeDescriptionValue = coreControl.getEntityIntegerRangeDescriptionValue(entityIntegerRangeDescription);
160        
161        entityIntegerRangeDescriptionValue.setDescription(edit.getDescription());
162
163        coreControl.updateEntityIntegerRangeDescriptionFromValue(entityIntegerRangeDescriptionValue, getPartyPK());
164    }
165    
166}