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.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.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class EditEntityIntegerRangeDescriptionCommand
048        extends BaseAbstractEditCommand<EntityIntegerRangeDescriptionSpec, EntityIntegerRangeDescriptionEdit, EditEntityIntegerRangeDescriptionResult, EntityIntegerRangeDescription, EntityIntegerRange> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.EntityIntegerRange.name(), SecurityRoles.Description.name())
059                        )))
060                )));
061        
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
065                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("EntityIntegerRangeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
068                ));
069        
070        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
071                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
072                ));
073    }
074    
075    /** Creates a new instance of EditEntityIntegerRangeDescriptionCommand */
076    public EditEntityIntegerRangeDescriptionCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079    
080    @Override
081    public EditEntityIntegerRangeDescriptionResult getResult() {
082        return CoreResultFactory.getEditEntityIntegerRangeDescriptionResult();
083    }
084
085    @Override
086    public EntityIntegerRangeDescriptionEdit getEdit() {
087        return CoreEditFactory.getEntityIntegerRangeDescriptionEdit();
088    }
089
090    @Override
091    public EntityIntegerRangeDescription getEntity(EditEntityIntegerRangeDescriptionResult result) {
092        EntityIntegerRangeDescription entityIntegerRangeDescription = null;
093        var componentVendorName = spec.getComponentVendorName();
094        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
095
096        if(componentVendor != null) {
097            var entityTypeName = spec.getEntityTypeName();
098            var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
099
100            if(entityType != null) {
101                var entityAttributeName = spec.getEntityAttributeName();
102                var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
103
104                if(entityAttribute != null) {
105                    var entityIntegerRangeName = spec.getEntityIntegerRangeName();
106                    var entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName);
107
108                    if(entityIntegerRange != null) {
109                        var partyControl = Session.getModelController(PartyControl.class);
110                        var languageIsoName = spec.getLanguageIsoName();
111                        var language = partyControl.getLanguageByIsoName(languageIsoName);
112
113                        if(language != null) {
114                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
115                                entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, language);
116                            } else { // EditMode.UPDATE
117                                entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescriptionForUpdate(entityIntegerRange, language);
118                            }
119
120                            if(entityIntegerRangeDescription == null) {
121                                addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName,
122                                        entityIntegerRangeName, languageIsoName);
123                            }
124                        } else {
125                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
126                        }
127                    } else {
128                        addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName);
129                    }
130                } else {
131                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
132                }
133            } else {
134                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
135            }
136        } else {
137            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
138        }
139
140        return entityIntegerRangeDescription;
141    }
142
143    @Override
144    public EntityIntegerRange getLockEntity(EntityIntegerRangeDescription entityIntegerRangeDescription) {
145        return entityIntegerRangeDescription.getEntityIntegerRange();
146    }
147
148    @Override
149    public void fillInResult(EditEntityIntegerRangeDescriptionResult result, EntityIntegerRangeDescription entityIntegerRangeDescription) {
150
151        result.setEntityIntegerRangeDescription(coreControl.getEntityIntegerRangeDescriptionTransfer(getUserVisit(), entityIntegerRangeDescription, null));
152    }
153
154    @Override
155    public void doLock(EntityIntegerRangeDescriptionEdit edit, EntityIntegerRangeDescription entityIntegerRangeDescription) {
156        edit.setDescription(entityIntegerRangeDescription.getDescription());
157    }
158
159    @Override
160    public void doUpdate(EntityIntegerRangeDescription entityIntegerRangeDescription) {
161        var entityIntegerRangeDescriptionValue = coreControl.getEntityIntegerRangeDescriptionValue(entityIntegerRangeDescription);
162        
163        entityIntegerRangeDescriptionValue.setDescription(edit.getDescription());
164
165        coreControl.updateEntityIntegerRangeDescriptionFromValue(entityIntegerRangeDescriptionValue, getPartyPK());
166    }
167    
168}