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.form.CreateEntityBlobAttributeForm;
020import com.echothree.model.control.core.common.EntityAttributeTypes;
021import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
022import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
023import com.echothree.model.control.core.server.logic.MimeTypeLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.logic.LanguageLogic;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.control.CommandSecurityDefinition;
033import com.echothree.util.server.control.PartyTypeDefinition;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036
037@Dependent
038public class CreateEntityBlobAttributeCommand
039        extends BaseSimpleCommand<CreateEntityBlobAttributeForm> {
040
041    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
046                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
047                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
048        ));
049
050        FORM_FIELD_DEFINITIONS = List.of(
051                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
052                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
053                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
054                new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null),
055                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
056                new FieldDefinition("LanguageUuid", FieldType.ENTITY_NAME, false, null, null),
057                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null)
058                );
059    }
060    
061    /** Creates a new instance of CreateEntityBlobAttributeCommand */
062    public CreateEntityBlobAttributeCommand() {
063        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
064    }
065    
066    @Override
067    protected BaseResult execute() {
068        var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
069
070        if(parameterCount == 1) {
071            var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form);
072
073            if(!hasExecutionErrors()) {
074                var entityAttributeName = form.getEntityAttributeName();
075                var entityAttributeUuid = form.getEntityAttributeUuid();
076                
077                parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1);
078                
079                if(parameterCount == 1) {
080                    var entityAttribute = entityAttributeName == null ?
081                            EntityAttributeLogic.getInstance().getEntityAttributeByUuid(this, entityAttributeUuid) :
082                            EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName);
083
084                    if(!hasExecutionErrors()) {
085                        var entityAttributeTypeName = entityAttribute.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName();
086
087                        if(EntityAttributeTypes.BLOB.name().equals(entityAttributeTypeName)) {
088                            if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) {
089                                var languageIsoName = form.getLanguageIsoName();
090                                var languageUuid = form.getLanguageUuid();
091
092                                parameterCount = (languageIsoName == null ? 0 : 1) + (languageUuid == null ? 0 : 1);
093
094                                if(parameterCount == 1) {
095                                    var language = languageIsoName == null ?
096                                            LanguageLogic.getInstance().getLanguageByUuid(this, languageUuid) :
097                                            LanguageLogic.getInstance().getLanguageByName(this, languageIsoName);
098
099                                    if(!hasExecutionErrors()) {
100                                        var entityBlobAttribute = coreControl.getEntityBlobAttribute(entityAttribute, entityInstance, language);
101
102                                        if(entityBlobAttribute == null) {
103                                            var mimeType = MimeTypeLogic.getInstance().getMimeTypeByName(this, form.getMimeTypeName());
104
105                                            if(!hasExecutionErrors()) {
106                                                if(mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.BLOB.name())) {
107                                                    var blobAttribute = form.getBlobAttribute();
108
109                                                    if(blobAttribute != null) {
110                                                        coreControl.createEntityBlobAttribute(entityAttribute, entityInstance, language, blobAttribute, mimeType, getPartyPK());
111                                                    } else {
112                                                        addExecutionError(ExecutionErrors.MissingBlobAttribute.name());
113                                                    }
114                                                } else {
115                                                    addExecutionError(ExecutionErrors.InvalidMimeType.name(), mimeType.getLastDetail().getMimeTypeName());
116                                                }
117                                            }
118                                        } else {
119                                            addExecutionError(ExecutionErrors.DuplicateEntityBlobAttribute.name(),
120                                                    EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance),
121                                                    entityAttribute.getLastDetail().getEntityAttributeName(),
122                                                    language.getLanguageIsoName());
123                                        }
124                                    }
125                                }
126                            } else {
127                                var expectedEntityTypeDetail = entityAttribute.getLastDetail().getEntityType().getLastDetail();
128                                var suppliedEntityTypeDetail = entityInstance.getEntityType().getLastDetail();
129
130                                addExecutionError(ExecutionErrors.MismatchedEntityType.name(),
131                                        expectedEntityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
132                                        expectedEntityTypeDetail.getEntityTypeName(),
133                                        suppliedEntityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(),
134                                        suppliedEntityTypeDetail.getEntityTypeName());
135                            }
136                        } else {
137                            addExecutionError(ExecutionErrors.MismatchedEntityAttributeType.name(),
138                                    EntityAttributeTypes.BLOB.name(), entityAttributeTypeName);
139                        }
140                    }
141                } else {
142                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
143                }
144            }
145        } else {
146            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
147        }
148        
149        return null;
150    }
151    
152}