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.EntityNameAttributeEdit;
021import com.echothree.control.user.core.common.form.EditEntityNameAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.spec.EntityNameAttributeSpec;
024import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
025import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.data.core.server.entity.EntityNameAttribute;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.persistence.PersistenceUtils;
038import java.util.List;
039import java.util.regex.Pattern;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditEntityNameAttributeCommand
045        extends BaseEditCommand<EntityNameAttributeSpec, EntityNameAttributeEdit> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
055        ));
056
057        SPEC_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
059                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
060                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null)
062        );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("NameAttribute", FieldType.ENTITY_NAME, true, null, null)
066        );
067    }
068
069    @Inject
070    EntityAttributeLogic entityAttributeLogic;
071
072    @Inject
073    EntityInstanceLogic entityInstanceLogic;
074
075    
076    /** Creates a new instance of EditEntityNameAttributeCommand */
077    public EditEntityNameAttributeCommand() {
078        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080    
081    @Override
082    protected BaseResult execute() {
083        var result = CoreResultFactory.getEditEntityNameAttributeResult();
084        var parameterCount = entityInstanceLogic.countPossibleEntitySpecs(spec);
085
086        if(parameterCount == 1) {
087            var entityInstance = entityInstanceLogic.getEntityInstance(this, spec);
088
089            if(!hasExecutionErrors()) {
090                var entityAttributeName = spec.getEntityAttributeName();
091                var entityAttributeUuid = spec.getEntityAttributeUuid();
092
093                parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1);
094
095                if(parameterCount == 1) {
096                    var entityAttribute = entityAttributeName == null ?
097                            entityAttributeLogic.getEntityAttributeByUuid(this, entityAttributeUuid) :
098                            entityAttributeLogic.getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName);
099
100                    if(!hasExecutionErrors()) {
101                        if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) {
102                            EntityNameAttribute entityNameAttribute = null;
103                            var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
104
105                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
106                                entityNameAttribute = coreControl.getEntityNameAttribute(entityAttribute, entityInstance);
107
108                                if(entityNameAttribute != null) {
109                                    if(editMode.equals(EditMode.LOCK)) {
110                                        result.setEntityNameAttribute(coreControl.getEntityNameAttributeTransfer(getUserVisit(),
111                                                entityNameAttribute, entityInstance));
112
113                                        if(lockEntity(basePK)) {
114                                            var edit = CoreEditFactory.getEntityNameAttributeEdit();
115
116                                            result.setEdit(edit);
117                                            edit.setNameAttribute(entityNameAttribute.getNameAttribute());
118                                        } else {
119                                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
120                                        }
121                                    } else { // EditMode.ABANDON
122                                        unlockEntity(basePK);
123                                        basePK = null;
124                                    }
125                                } else {
126                                    addExecutionError(ExecutionErrors.UnknownEntityNameAttribute.name(),
127                                            entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
128                                }
129                            } else if(editMode.equals(EditMode.UPDATE)) {
130                                var entityAttributeString = coreControl.getEntityAttributeString(entityAttribute);
131                                var validationPattern = entityAttributeString == null ? null : entityAttributeString.getValidationPattern();
132                                var stringAttribute = edit.getNameAttribute();
133
134                                if(validationPattern != null) {
135                                    var pattern = Pattern.compile(validationPattern);
136                                    var matcher = pattern.matcher(stringAttribute);
137
138                                    if(!matcher.matches()) {
139                                        addExecutionError(ExecutionErrors.InvalidNameAttribute.name(), stringAttribute);
140                                    }
141                                }
142
143                                if(!hasExecutionErrors()) {
144                                    entityNameAttribute = coreControl.getEntityNameAttributeForUpdate(entityAttribute, entityInstance);
145
146                                    if(entityNameAttribute != null) {
147                                        if(lockEntityForUpdate(basePK)) {
148                                            try {
149                                                var entityNameAttributeValue = coreControl.getEntityNameAttributeValueForUpdate(entityNameAttribute);
150
151                                                entityNameAttributeValue.setNameAttribute(stringAttribute);
152
153                                                coreControl.updateEntityNameAttributeFromValue(entityNameAttributeValue, getPartyPK());
154                                            } finally {
155                                                unlockEntity(basePK);
156                                                basePK = null;
157                                            }
158                                        } else {
159                                            addExecutionError(ExecutionErrors.EntityLockStale.name());
160                                        }
161                                    } else {
162                                        addExecutionError(ExecutionErrors.UnknownEntityNameAttribute.name(),
163                                                entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
164                                    }
165                                }
166                            }
167
168                            if(basePK != null) {
169                                result.setEntityLock(getEntityLockTransfer(basePK));
170                            }
171
172                            if(entityNameAttribute != null) {
173                                result.setEntityNameAttribute(coreControl.getEntityNameAttributeTransfer(getUserVisit(), entityNameAttribute, entityInstance));
174                            }
175                        } else {
176                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
177                        }
178                    }
179                } else {
180                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
181                }
182            }
183        } else {
184            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
185        }
186
187        return result;
188    }
189    
190}