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.EntityLongRangeDescriptionEdit;
021import com.echothree.control.user.core.common.form.EditEntityLongRangeDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityLongRangeDescriptionResult;
024import com.echothree.control.user.core.common.spec.EntityLongRangeDescriptionSpec;
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.EntityLongRange;
030import com.echothree.model.data.core.server.entity.EntityLongRangeDescription;
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 java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditEntityLongRangeDescriptionCommand
046        extends BaseAbstractEditCommand<EntityLongRangeDescriptionSpec, EntityLongRangeDescriptionEdit, EditEntityLongRangeDescriptionResult, EntityLongRangeDescription, 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(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.EntityLongRange.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("EntityLongRangeName", 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    @Inject
074    PartyControl partyControl;
075
076    
077    /** Creates a new instance of EditEntityLongRangeDescriptionCommand */
078    public EditEntityLongRangeDescriptionCommand() {
079        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditEntityLongRangeDescriptionResult getResult() {
084        return CoreResultFactory.getEditEntityLongRangeDescriptionResult();
085    }
086
087    @Override
088    public EntityLongRangeDescriptionEdit getEdit() {
089        return CoreEditFactory.getEntityLongRangeDescriptionEdit();
090    }
091
092    @Override
093    public EntityLongRangeDescription getEntity(EditEntityLongRangeDescriptionResult result) {
094        EntityLongRangeDescription entityLongRangeDescription = null;
095        var componentVendorName = spec.getComponentVendorName();
096        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
097
098        if(componentVendor != null) {
099            var entityTypeName = spec.getEntityTypeName();
100            var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
101
102            if(entityType != null) {
103                var entityAttributeName = spec.getEntityAttributeName();
104                var entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
105
106                if(entityAttribute != null) {
107                    var entityLongRangeName = spec.getEntityLongRangeName();
108                    var entityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName);
109
110                    if(entityLongRange != null) {
111                        var languageIsoName = spec.getLanguageIsoName();
112                        var language = partyControl.getLanguageByIsoName(languageIsoName);
113
114                        if(language != null) {
115                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
116                                entityLongRangeDescription = coreControl.getEntityLongRangeDescription(entityLongRange, language);
117                            } else { // EditMode.UPDATE
118                                entityLongRangeDescription = coreControl.getEntityLongRangeDescriptionForUpdate(entityLongRange, language);
119                            }
120
121                            if(entityLongRangeDescription == null) {
122                                addExecutionError(ExecutionErrors.UnknownEntityLongRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName,
123                                        entityLongRangeName, languageIsoName);
124                            }
125                        } else {
126                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
127                        }
128                    } else {
129                        addExecutionError(ExecutionErrors.UnknownEntityLongRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityLongRangeName);
130                    }
131                } else {
132                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
133                }
134            } else {
135                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
136            }
137        } else {
138            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
139        }
140
141        return entityLongRangeDescription;
142    }
143
144    @Override
145    public EntityLongRange getLockEntity(EntityLongRangeDescription entityLongRangeDescription) {
146        return entityLongRangeDescription.getEntityLongRange();
147    }
148
149    @Override
150    public void fillInResult(EditEntityLongRangeDescriptionResult result, EntityLongRangeDescription entityLongRangeDescription) {
151
152        result.setEntityLongRangeDescription(coreControl.getEntityLongRangeDescriptionTransfer(getUserVisit(), entityLongRangeDescription, null));
153    }
154
155    @Override
156    public void doLock(EntityLongRangeDescriptionEdit edit, EntityLongRangeDescription entityLongRangeDescription) {
157        edit.setDescription(entityLongRangeDescription.getDescription());
158    }
159
160    @Override
161    public void doUpdate(EntityLongRangeDescription entityLongRangeDescription) {
162        var entityLongRangeDescriptionValue = coreControl.getEntityLongRangeDescriptionValue(entityLongRangeDescription);
163        
164        entityLongRangeDescriptionValue.setDescription(edit.getDescription());
165
166        coreControl.updateEntityLongRangeDescriptionFromValue(entityLongRangeDescriptionValue, getPartyPK());
167    }
168    
169}