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.EntityStringAttributeEdit;
021import com.echothree.control.user.core.common.form.EditEntityStringAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.spec.EntityStringAttributeSpec;
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.control.party.server.logic.LanguageLogic;
028import com.echothree.model.data.core.server.entity.EntityStringAttribute;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.server.control.BaseEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.persistence.PersistenceUtils;
039import java.util.List;
040import java.util.regex.Pattern;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditEntityStringAttributeCommand
046        extends BaseEditCommand<EntityStringAttributeSpec, EntityStringAttributeEdit> {
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(), null)
056        ));
057
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
060                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
061                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null),
063                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("LanguageUuid", FieldType.ENTITY_NAME, false, null, null)
065        );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("StringAttribute", FieldType.STRING, true, 1L, 512L)
069        );
070    }
071
072    @Inject
073    EntityAttributeLogic entityAttributeLogic;
074
075    @Inject
076    EntityInstanceLogic entityInstanceLogic;
077
078    @Inject
079    LanguageLogic languageLogic;
080
081    
082    /** Creates a new instance of EditEntityStringAttributeCommand */
083    public EditEntityStringAttributeCommand() {
084        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086    
087    @Override
088    protected BaseResult execute() {
089        var result = CoreResultFactory.getEditEntityStringAttributeResult();
090        var parameterCount = entityInstanceLogic.countPossibleEntitySpecs(spec);
091
092        if(parameterCount == 1) {
093            var entityInstance = entityInstanceLogic.getEntityInstance(this, spec);
094
095            if(!hasExecutionErrors()) {
096                var entityAttributeName = spec.getEntityAttributeName();
097                var entityAttributeUuid = spec.getEntityAttributeUuid();
098                
099                parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1);
100                
101                if(parameterCount == 1) {
102                    var entityAttribute = entityAttributeName == null ?
103                            entityAttributeLogic.getEntityAttributeByUuid(this, entityAttributeUuid) :
104                            entityAttributeLogic.getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName);
105
106                    if(!hasExecutionErrors()) {
107                        if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) {
108                            var languageIsoName = spec.getLanguageIsoName();
109                            var languageUuid = spec.getLanguageUuid();
110                            
111                            parameterCount = (languageIsoName == null ? 0 : 1) + (languageUuid == null ? 0 : 1);
112
113                            if(parameterCount == 1) {
114                                var language = languageIsoName == null ?
115                                        languageLogic.getLanguageByUuid(this, languageUuid) :
116                                        languageLogic.getLanguageByName(this, languageIsoName);
117
118                                if(!hasExecutionErrors()) {
119                                    EntityStringAttribute entityStringAttribute = null;
120                                    var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
121
122                                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
123                                        entityStringAttribute = coreControl.getEntityStringAttribute(entityAttribute, entityInstance, language);
124
125                                        if(entityStringAttribute != null) {
126                                            if(editMode.equals(EditMode.LOCK)) {
127                                                result.setEntityStringAttribute(coreControl.getEntityStringAttributeTransfer(getUserVisit(),
128                                                        entityStringAttribute, entityInstance));
129
130                                                if(lockEntity(basePK)) {
131                                                    var edit = CoreEditFactory.getEntityStringAttributeEdit();
132
133                                                    result.setEdit(edit);
134                                                    edit.setStringAttribute(entityStringAttribute.getStringAttribute());
135                                                } else {
136                                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
137                                                }
138                                            } else { // EditMode.ABANDON
139                                                unlockEntity(basePK);
140                                                basePK = null;
141                                            }
142                                        } else {
143                                            addExecutionError(ExecutionErrors.UnknownEntityStringAttribute.name(),
144                                                    entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
145                                        }
146                                    } else if(editMode.equals(EditMode.UPDATE)) {
147                                        var entityAttributeString = coreControl.getEntityAttributeString(entityAttribute);
148                                        var validationPattern = entityAttributeString == null ? null : entityAttributeString.getValidationPattern();
149                                        var stringAttribute = edit.getStringAttribute();
150
151                                        if(validationPattern != null) {
152                                            var pattern = Pattern.compile(validationPattern);
153                                            var m = pattern.matcher(stringAttribute);
154
155                                            if(!m.matches()) {
156                                                addExecutionError(ExecutionErrors.InvalidStringAttribute.name(), stringAttribute);
157                                            }
158                                        }
159
160                                        if(!hasExecutionErrors()) {
161                                            entityStringAttribute = coreControl.getEntityStringAttributeForUpdate(entityAttribute, entityInstance, language);
162
163                                            if(entityStringAttribute != null) {
164                                                if(lockEntityForUpdate(basePK)) {
165                                                    try {
166                                                        var entityStringAttributeValue = coreControl.getEntityStringAttributeValueForUpdate(entityStringAttribute);
167
168                                                        entityStringAttributeValue.setStringAttribute(stringAttribute);
169
170                                                        coreControl.updateEntityStringAttributeFromValue(entityStringAttributeValue, getPartyPK());
171                                                    } finally {
172                                                        unlockEntity(basePK);
173                                                        basePK = null;
174                                                    }
175                                                } else {
176                                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
177                                                }
178                                            } else {
179                                                addExecutionError(ExecutionErrors.UnknownEntityStringAttribute.name(),
180                                                        entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
181                                            }
182                                        }
183                                    }
184
185                                    if(basePK != null) {
186                                        result.setEntityLock(getEntityLockTransfer(basePK));
187                                    }
188
189                                    if(entityStringAttribute != null) {
190                                        result.setEntityStringAttribute(coreControl.getEntityStringAttributeTransfer(getUserVisit(), entityStringAttribute, entityInstance));
191                                    }
192                                }
193                            } else {
194                                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
195                            }
196                      } else {
197                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
198                        }
199                    }
200                } else {
201                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
202                }
203            }
204        } else {
205            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
206        }
207
208        return result;
209    }
210    
211}