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.EntityBlobAttributeEdit;
021import com.echothree.control.user.core.common.form.EditEntityBlobAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.spec.EntityBlobAttributeSpec;
024import com.echothree.model.control.core.common.EntityAttributeTypes;
025import com.echothree.model.control.core.server.control.EntityInstanceControl;
026import com.echothree.model.control.core.server.logic.MimeTypeLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.data.core.server.entity.EntityBlobAttribute;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.command.EditMode;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.persistence.PersistenceUtils;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditEntityBlobAttributeCommand
046        extends BaseEditCommand<EntityBlobAttributeSpec, EntityBlobAttributeEdit> {
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, true, null, null),
060                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
062        );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null)
066        );
067    }
068
069    @Inject
070    EntityInstanceControl entityInstanceControl;
071
072    @Inject
073    PartyControl partyControl;
074
075    @Inject
076    MimeTypeLogic mimeTypeLogic;
077
078    
079    /** Creates a new instance of EditEntityBlobAttributeCommand */
080    public EditEntityBlobAttributeCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083    
084    @Override
085    protected BaseResult execute() {
086        var result = CoreResultFactory.getEditEntityBlobAttributeResult();
087        var entityRef = spec.getEntityRef();
088        var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef);
089
090        if(entityInstance != null) {
091            var entityAttributeName = spec.getEntityAttributeName();
092            var entityAttribute = coreControl.getEntityAttributeByName(entityInstance.getEntityType(), entityAttributeName);
093
094            if(entityAttribute != null) {
095                var languageIsoName = spec.getLanguageIsoName();
096                var language = partyControl.getLanguageByIsoName(languageIsoName);
097
098                if(language != null) {
099                    EntityBlobAttribute entityBlobAttribute = null;
100                    var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
101                    
102                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
103                        entityBlobAttribute = coreControl.getEntityBlobAttribute(entityAttribute, entityInstance, language);
104
105                        if(entityBlobAttribute != null) {
106                            if(editMode.equals(EditMode.LOCK)) {
107                                result.setEntityBlobAttribute(coreControl.getEntityBlobAttributeTransfer(getUserVisit(),
108                                        entityBlobAttribute, entityInstance));
109
110                                if(lockEntity(basePK)) {
111                                    var edit = CoreEditFactory.getEntityBlobAttributeEdit();
112
113                                    result.setEdit(edit);
114                                    edit.setMimeTypeName(entityBlobAttribute.getMimeType().getLastDetail().getMimeTypeName());
115                                } else {
116                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
117                                }
118                            } else { // EditMode.ABANDON
119                                unlockEntity(basePK);
120                                basePK = null;
121                            }
122                        } else {
123                            var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
124
125                            addExecutionError(ExecutionErrors.UnknownEntityBlobAttribute.name(), entityRef,
126                                    entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
127                                    entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName);
128                        }
129                    } else if(editMode.equals(EditMode.UPDATE)) {
130                        entityBlobAttribute = coreControl.getEntityBlobAttributeForUpdate(entityAttribute, entityInstance, language);
131
132                        if(entityBlobAttribute != null) {
133                            var mimeType = mimeTypeLogic.getMimeTypeByName(this, edit.getMimeTypeName());
134
135                            if(!hasExecutionErrors()) {
136                                if(mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.BLOB.name())) {
137                                    var blobAttribute = edit.getBlobAttribute();
138
139                                    if(blobAttribute != null) {
140                                        if(lockEntityForUpdate(basePK)) {
141                                            try {
142                                                var entityBlobAttributeValue = coreControl.getEntityBlobAttributeValueForUpdate(entityBlobAttribute);
143
144                                                entityBlobAttributeValue.setBlobAttribute(blobAttribute);
145                                                entityBlobAttributeValue.setMimeTypePK(mimeType.getPrimaryKey());
146
147                                                coreControl.updateEntityBlobAttributeFromValue(entityBlobAttributeValue, getPartyPK());
148                                            } finally {
149                                                unlockEntity(basePK);
150                                                basePK = null;
151                                            }
152                                        } else {
153                                            addExecutionError(ExecutionErrors.EntityLockStale.name());
154                                        }
155                                    } else {
156                                        addExecutionError(ExecutionErrors.MissingBlobAttribute.name());
157                                    }
158                                } else {
159                                    addExecutionError(ExecutionErrors.InvalidMimeType.name(), mimeType.getLastDetail().getMimeTypeName());
160                                }
161                            }
162                        } else {
163                            var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
164
165                            addExecutionError(ExecutionErrors.UnknownEntityBlobAttribute.name(), entityRef,
166                                    entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
167                                    entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName);
168                        }
169                    }
170                    
171                    if(basePK != null) {
172                        result.setEntityLock(getEntityLockTransfer(basePK));
173                    }
174
175                    if(entityBlobAttribute != null) {
176                        result.setEntityBlobAttribute(coreControl.getEntityBlobAttributeTransfer(getUserVisit(), entityBlobAttribute, entityInstance));
177                    }
178                } else {
179                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
180                }
181            } else {
182                var entityTypeDetail = entityInstance.getEntityType().getLastDetail();
183
184                addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
185                        entityTypeDetail.getEntityTypeName(), entityAttributeName);
186            }
187        } else {
188            addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef);
189        }
190
191        return result;
192    }
193    
194}