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.EntityLongAttributeEdit;
021import com.echothree.control.user.core.common.form.EditEntityLongAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditEntityLongAttributeResult;
024import com.echothree.control.user.core.common.spec.EntityLongAttributeSpec;
025import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
026import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.data.core.server.entity.EntityAttribute;
029import com.echothree.model.data.core.server.entity.EntityAttributeLong;
030import com.echothree.model.data.core.server.entity.EntityInstance;
031import com.echothree.model.data.core.server.entity.EntityLongAttribute;
032import com.echothree.model.data.core.server.value.EntityLongAttributeValue;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.common.persistence.BasePK;
040import com.echothree.util.server.control.BaseEditCommand;
041import com.echothree.util.server.control.CommandSecurityDefinition;
042import com.echothree.util.server.control.PartyTypeDefinition;
043import com.echothree.util.server.persistence.PersistenceUtils;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047
048public class EditEntityLongAttributeCommand
049        extends BaseEditCommand<EntityLongAttributeSpec, EntityLongAttributeEdit> {
050
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054    
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
059        ));
060
061        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Key", FieldType.KEY, false, null, null),
064                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
065                new FieldDefinition("Ulid", FieldType.ULID, false, null, null),
066                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("EntityAttributeUlid", FieldType.ULID, false, null, null)
068                ));
069        
070        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
071                new FieldDefinition("LongAttribute", FieldType.SIGNED_LONG, true, null, null)
072                ));
073    }
074    
075    /** Creates a new instance of EditEntityLongAttributeCommand */
076    public EditEntityLongAttributeCommand(UserVisitPK userVisitPK, EditEntityLongAttributeForm form) {
077        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079    
080    @Override
081    protected BaseResult execute() {
082        EditEntityLongAttributeResult result = CoreResultFactory.getEditEntityLongAttributeResult();
083        var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(spec);
084
085        if(parameterCount == 1) {
086            var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, spec);
087
088            if(!hasExecutionErrors()) {
089                String entityAttributeName = spec.getEntityAttributeName();
090                String entityAttributeUlid = spec.getEntityAttributeUlid();
091                
092                parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUlid == null ? 0 : 1);
093                
094                if(parameterCount == 1) {
095                    EntityAttribute entityAttribute = entityAttributeName == null ?
096                            EntityAttributeLogic.getInstance().getEntityAttributeByUlid(this, entityAttributeUlid) :
097                            EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName);
098
099                    if(!hasExecutionErrors()) {
100                        if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) {
101                            var coreControl = getCoreControl();
102                            EntityLongAttribute entityLongAttribute = null;
103                            BasePK basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
104
105                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
106                                entityLongAttribute = coreControl.getEntityLongAttribute(entityAttribute, entityInstance);
107
108                                if(entityLongAttribute != null) {
109                                    if(editMode.equals(EditMode.LOCK)) {
110                                        result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance));
111
112                                        if(lockEntity(basePK)) {
113                                            EntityLongAttributeEdit edit = CoreEditFactory.getEntityLongAttributeEdit();
114
115                                            result.setEdit(edit);
116                                            edit.setLongAttribute(entityLongAttribute.getLongAttribute().toString());
117                                        } else {
118                                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
119                                        }
120                                    } else { // EditMode.ABANDON
121                                        unlockEntity(basePK);
122                                        basePK = null;
123                                    }
124                                } else {
125                                    addExecutionError(ExecutionErrors.UnknownEntityLongAttribute.name(),
126                                            EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
127                                }
128                            } else if(editMode.equals(EditMode.UPDATE)) {
129                                EntityAttributeLong entityAttributeLong = coreControl.getEntityAttributeLong(entityAttribute);
130                                Long longAttribute = Long.valueOf(edit.getLongAttribute());
131
132                                if(entityAttributeLong != null) {
133                                    Long upperRangeLongValue = entityAttributeLong.getUpperRangeLongValue();
134                                    Long lowerRangeLongValue = entityAttributeLong.getLowerRangeLongValue();
135
136                                    if(upperRangeLongValue != null && longAttribute > upperRangeLongValue){
137                                        addExecutionError(ExecutionErrors.UpperRangeExceeded.name(),
138                                                upperRangeLongValue, longAttribute);
139                                    }
140
141                                    if(lowerRangeLongValue != null && longAttribute < lowerRangeLongValue) {
142                                        addExecutionError(ExecutionErrors.LowerRangeExceeded.name(),
143                                                lowerRangeLongValue, longAttribute);
144                                    }
145                                }
146
147
148                                if(!hasExecutionErrors()) {
149                                    entityLongAttribute = coreControl.getEntityLongAttributeForUpdate(entityAttribute, entityInstance);
150
151                                    if(entityLongAttribute != null) {
152                                        if(lockEntityForUpdate(basePK)) {
153                                            try {
154                                                EntityLongAttributeValue entityLongAttributeValue = coreControl.getEntityLongAttributeValueForUpdate(entityLongAttribute);
155
156                                                entityLongAttributeValue.setLongAttribute(longAttribute);
157
158                                                coreControl.updateEntityLongAttributeFromValue(entityLongAttributeValue, getPartyPK());
159                                            } finally {
160                                                unlockEntity(basePK);
161                                                basePK = null;
162                                            }
163                                        } else {
164                                            addExecutionError(ExecutionErrors.EntityLockStale.name());
165                                        }
166                                    } else {
167                                        addExecutionError(ExecutionErrors.UnknownEntityLongAttribute.name(),
168                                                EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance),
169                                                entityAttribute.getLastDetail().getEntityAttributeName());
170                                    }
171                                }
172                            }
173
174                            if(basePK != null) {
175                                result.setEntityLock(getEntityLockTransfer(basePK));
176                            }
177
178                            if(entityLongAttribute != null) {
179                                result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance));
180                            }
181                        } else {
182                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
183                        }
184                    }
185                } else {
186                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
187                }
188            }
189        } else {
190            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
191        }
192
193        return result;
194    }
195    
196}