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