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.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.ComponentVendor;
030import com.echothree.model.data.core.server.entity.EntityAttribute;
031import com.echothree.model.data.core.server.entity.EntityIntegerRange;
032import com.echothree.model.data.core.server.entity.EntityIntegerRangeDescription;
033import com.echothree.model.data.core.server.entity.EntityType;
034import com.echothree.model.data.core.server.value.EntityIntegerRangeDescriptionValue;
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 EditEntityIntegerRangeDescriptionCommand
051        extends BaseAbstractEditCommand<EntityIntegerRangeDescriptionSpec, EntityIntegerRangeDescriptionEdit, EditEntityIntegerRangeDescriptionResult, EntityIntegerRangeDescription, EntityIntegerRange> {
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.EntityIntegerRange.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("EntityIntegerRangeName", 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 EditEntityIntegerRangeDescriptionCommand */
079    public EditEntityIntegerRangeDescriptionCommand(UserVisitPK userVisitPK, EditEntityIntegerRangeDescriptionForm form) {
080        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    public EditEntityIntegerRangeDescriptionResult getResult() {
085        return CoreResultFactory.getEditEntityIntegerRangeDescriptionResult();
086    }
087
088    @Override
089    public EntityIntegerRangeDescriptionEdit getEdit() {
090        return CoreEditFactory.getEntityIntegerRangeDescriptionEdit();
091    }
092
093    @Override
094    public EntityIntegerRangeDescription getEntity(EditEntityIntegerRangeDescriptionResult result) {
095        var coreControl = getCoreControl();
096        EntityIntegerRangeDescription entityIntegerRangeDescription = 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 entityIntegerRangeName = spec.getEntityIntegerRangeName();
110                    EntityIntegerRange entityIntegerRange = coreControl.getEntityIntegerRangeByName(entityAttribute, entityIntegerRangeName);
111
112                    if(entityIntegerRange != 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                                entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescription(entityIntegerRange, language);
120                            } else { // EditMode.UPDATE
121                                entityIntegerRangeDescription = coreControl.getEntityIntegerRangeDescriptionForUpdate(entityIntegerRange, language);
122                            }
123
124                            if(entityIntegerRangeDescription == null) {
125                                addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeDescription.name(), componentVendorName, entityTypeName, entityAttributeName,
126                                        entityIntegerRangeName, languageIsoName);
127                            }
128                        } else {
129                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
130                        }
131                    } else {
132                        addExecutionError(ExecutionErrors.UnknownEntityIntegerRangeName.name(), componentVendorName, entityTypeName, entityAttributeName, entityIntegerRangeName);
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 entityIntegerRangeDescription;
145    }
146
147    @Override
148    public EntityIntegerRange getLockEntity(EntityIntegerRangeDescription entityIntegerRangeDescription) {
149        return entityIntegerRangeDescription.getEntityIntegerRange();
150    }
151
152    @Override
153    public void fillInResult(EditEntityIntegerRangeDescriptionResult result, EntityIntegerRangeDescription entityIntegerRangeDescription) {
154        var coreControl = getCoreControl();
155
156        result.setEntityIntegerRangeDescription(coreControl.getEntityIntegerRangeDescriptionTransfer(getUserVisit(), entityIntegerRangeDescription, null));
157    }
158
159    @Override
160    public void doLock(EntityIntegerRangeDescriptionEdit edit, EntityIntegerRangeDescription entityIntegerRangeDescription) {
161        edit.setDescription(entityIntegerRangeDescription.getDescription());
162    }
163
164    @Override
165    public void doUpdate(EntityIntegerRangeDescription entityIntegerRangeDescription) {
166        var coreControl = getCoreControl();
167        EntityIntegerRangeDescriptionValue entityIntegerRangeDescriptionValue = coreControl.getEntityIntegerRangeDescriptionValue(entityIntegerRangeDescription);
168        
169        entityIntegerRangeDescriptionValue.setDescription(edit.getDescription());
170
171        coreControl.updateEntityIntegerRangeDescriptionFromValue(entityIntegerRangeDescriptionValue, getPartyPK());
172    }
173    
174}