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.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.ComponentVendor;
030import com.echothree.model.data.core.server.entity.EntityAttribute;
031import com.echothree.model.data.core.server.entity.EntityLongRange;
032import com.echothree.model.data.core.server.entity.EntityLongRangeDescription;
033import com.echothree.model.data.core.server.entity.EntityType;
034import com.echothree.model.data.core.server.value.EntityLongRangeDescriptionValue;
035import com.echothree.model.data.party.server.entity.Language;
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 com.echothree.util.server.persistence.Session;
046import java.util.Arrays;
047import java.util.Collections;
048import java.util.List;
049
050public class EditEntityLongRangeDescriptionCommand
051        extends BaseAbstractEditCommand<EntityLongRangeDescriptionSpec, EntityLongRangeDescriptionEdit, EditEntityLongRangeDescriptionResult, EntityLongRangeDescription, EntityLongRange> {
052    
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
055    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
056    
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                        new SecurityRoleDefinition(SecurityRoleGroups.EntityLongRange.name(), SecurityRoles.Description.name())
062                        )))
063                )));
064        
065        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
068                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("EntityLongRangeName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
071                ));
072        
073        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
074                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
075                ));
076    }
077    
078    /** Creates a new instance of EditEntityLongRangeDescriptionCommand */
079    public EditEntityLongRangeDescriptionCommand(UserVisitPK userVisitPK, EditEntityLongRangeDescriptionForm form) {
080        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    public EditEntityLongRangeDescriptionResult getResult() {
085        return CoreResultFactory.getEditEntityLongRangeDescriptionResult();
086    }
087
088    @Override
089    public EntityLongRangeDescriptionEdit getEdit() {
090        return CoreEditFactory.getEntityLongRangeDescriptionEdit();
091    }
092
093    @Override
094    public EntityLongRangeDescription getEntity(EditEntityLongRangeDescriptionResult result) {
095        var coreControl = getCoreControl();
096        EntityLongRangeDescription entityLongRangeDescription = null;
097        String componentVendorName = spec.getComponentVendorName();
098        ComponentVendor componentVendor = coreControl.getComponentVendorByName(componentVendorName);
099
100        if(componentVendor != null) {
101            String entityTypeName = spec.getEntityTypeName();
102            EntityType entityType = coreControl.getEntityTypeByName(componentVendor, entityTypeName);
103
104            if(entityType != null) {
105                String entityAttributeName = spec.getEntityAttributeName();
106                EntityAttribute entityAttribute = coreControl.getEntityAttributeByName(entityType, entityAttributeName);
107
108                if(entityAttribute != null) {
109                    String entityLongRangeName = spec.getEntityLongRangeName();
110                    EntityLongRange entityLongRange = coreControl.getEntityLongRangeByName(entityAttribute, entityLongRangeName);
111
112                    if(entityLongRange != null) {
113                        var partyControl = Session.getModelController(PartyControl.class);
114                        String languageIsoName = spec.getLanguageIsoName();
115                        Language language = partyControl.getLanguageByIsoName(languageIsoName);
116
117                        if(language != null) {
118                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
119                                entityLongRangeDescription = coreControl.getEntityLongRangeDescription(entityLongRange, language);
120                            } else { // EditMode.UPDATE
121                                entityLongRangeDescription = coreControl.getEntityLongRangeDescriptionForUpdate(entityLongRange, language);
122                            }
123
124                            if(entityLongRangeDescription == null) {
125                                addExecutionError(ExecutionErrors.UnknownEntityLongRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName,
126                                        entityLongRangeName, languageIsoName);
127                            }
128                        } else {
129                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
130                        }
131                    } else {
132                        addExecutionError(ExecutionErrors.UnknownEntityLongRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityLongRangeName);
133                    }
134                } else {
135                    addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), componentVendorName, entityTypeName, entityAttributeName);
136                }
137            } else {
138                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), componentVendorName, entityTypeName);
139            }
140        } else {
141            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
142        }
143
144        return entityLongRangeDescription;
145    }
146
147    @Override
148    public EntityLongRange getLockEntity(EntityLongRangeDescription entityLongRangeDescription) {
149        return entityLongRangeDescription.getEntityLongRange();
150    }
151
152    @Override
153    public void fillInResult(EditEntityLongRangeDescriptionResult result, EntityLongRangeDescription entityLongRangeDescription) {
154        var coreControl = getCoreControl();
155
156        result.setEntityLongRangeDescription(coreControl.getEntityLongRangeDescriptionTransfer(getUserVisit(), entityLongRangeDescription, null));
157    }
158
159    @Override
160    public void doLock(EntityLongRangeDescriptionEdit edit, EntityLongRangeDescription entityLongRangeDescription) {
161        edit.setDescription(entityLongRangeDescription.getDescription());
162    }
163
164    @Override
165    public void doUpdate(EntityLongRangeDescription entityLongRangeDescription) {
166        var coreControl = getCoreControl();
167        EntityLongRangeDescriptionValue entityLongRangeDescriptionValue = coreControl.getEntityLongRangeDescriptionValue(entityLongRangeDescription);
168        
169        entityLongRangeDescriptionValue.setDescription(edit.getDescription());
170
171        coreControl.updateEntityLongRangeDescriptionFromValue(entityLongRangeDescriptionValue, getPartyPK());
172    }
173    
174}