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.document.server.command;
018
019import com.echothree.control.user.document.common.form.CreatePartyDocumentForm;
020import com.echothree.control.user.document.common.result.CreatePartyDocumentResult;
021import com.echothree.control.user.document.common.result.DocumentResultFactory;
022import com.echothree.model.control.core.common.EntityAttributeTypes;
023import com.echothree.model.control.core.server.control.MimeTypeControl;
024import com.echothree.model.control.document.server.control.DocumentControl;
025import com.echothree.model.control.document.server.logic.DocumentLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.core.server.entity.MimeType;
031import com.echothree.model.data.document.server.entity.DocumentType;
032import com.echothree.model.data.party.server.entity.Party;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.persistence.type.ByteArray;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.server.control.BaseSimpleCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047import javax.enterprise.context.RequestScoped;
048
049@RequestScoped
050public class CreatePartyDocumentCommand
051        extends BaseSimpleCommand<CreatePartyDocumentForm> {
052    
053    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
054    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
055    
056    static {
057        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
058                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
059                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
060                    new SecurityRoleDefinition(SecurityRoleGroups.PartyDocument.name(), SecurityRoles.Create.name())
061                    )))
062                )));
063        
064        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("DocumentTypeName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null),
068                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L),
071                new FieldDefinition("Clob", FieldType.STRING, false, 1L, null)
072                ));
073    }
074    
075    /** Creates a new instance of CreatePartyDocumentCommand */
076    public CreatePartyDocumentCommand() {
077        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
078    }
079
080    private void createPartyDocument(Party party, DocumentType documentType, MimeType mimeType, ByteArray blob, String clob, CreatePartyDocumentResult result) {
081        var isDefault = Boolean.valueOf(form.getIsDefault());
082        var sortOrder = Integer.valueOf(form.getSortOrder());
083        var description = form.getDescription();
084
085        var partyDocument = DocumentLogic.getInstance().createPartyDocument(this, party, documentType, mimeType, isDefault, sortOrder,
086                getPreferredLanguage(), description, blob, clob, getPartyPK());
087
088        if(!hasExecutionErrors()) {
089            var document = partyDocument.getDocument();
090
091            result.setEntityRef(document.getPrimaryKey().getEntityRef());
092            result.setDocumentName(document.getLastDetail().getDocumentName());
093        }
094    }
095
096    @Override
097    protected BaseResult execute() {
098        var partyControl = Session.getModelController(PartyControl.class);
099        var result = DocumentResultFactory.getCreatePartyDocumentResult();
100        var partyName = form.getPartyName();
101        var party = partyControl.getPartyByName(partyName);
102
103        if(party != null) {
104            var documentControl = Session.getModelController(DocumentControl.class);
105            var documentTypeName = form.getDocumentTypeName();
106            var documentType = documentControl.getDocumentTypeByName(documentTypeName);
107
108            if(documentType != null) {
109                var documentTypeUsages = documentControl.getDocumentTypeUsagesByDocumentType(documentType);
110                var partyType = party.getLastDetail().getPartyType();
111                Integer maximumInstances = null;
112                var foundPartyTypeDocumentTypeUsageType = false;
113
114                for(var documentTypeUsage : documentTypeUsages) {
115                    var documentTypeUsageType = documentTypeUsage.getDocumentTypeUsageType();
116                    var partyTypeDocumentTypeUsageType = documentControl.getPartyTypeDocumentTypeUsageType(partyType, documentTypeUsageType);
117
118                    if(partyTypeDocumentTypeUsageType != null) {
119                        foundPartyTypeDocumentTypeUsageType = true;
120
121                        if(maximumInstances == null) {
122                            maximumInstances = documentTypeUsage.getMaximumInstances();
123                        } else {
124                            var foundMaximumInstances = documentTypeUsage.getMaximumInstances();
125
126                            if(foundMaximumInstances != null && foundMaximumInstances > maximumInstances) {
127                                maximumInstances = foundMaximumInstances;
128                            }
129                        }
130                    }
131                }
132
133                if(foundPartyTypeDocumentTypeUsageType) {
134                    if(maximumInstances != null) {
135                        Long instances = documentControl.countPartyDocumentsByPartyAndDocumentType(party, documentType);
136
137                        if(instances >= maximumInstances) {
138                            addExecutionError(ExecutionErrors.MaximumInstancesExceeded.name());
139                        }
140                    }
141
142                    if(!hasExecutionErrors()) {
143                        var mimeTypeControl = Session.getModelController(MimeTypeControl.class);
144                        var mimeTypeName = form.getMimeTypeName();
145                        var mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName);
146
147                        if(mimeType != null) {
148                            var mimeTypeUsageType = documentType.getLastDetail().getMimeTypeUsageType();
149                            var mimeTypeUsage = mimeTypeUsageType == null ? null : mimeTypeControl.getMimeTypeUsage(mimeType, mimeTypeUsageType);
150
151                            if(mimeTypeUsageType == null || mimeTypeUsage != null) {
152                                var entityAttributeType = mimeType.getLastDetail().getEntityAttributeType();
153                                var entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName();
154
155                                if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) {
156                                    var blob = form.getBlob();
157
158                                    if(blob != null) {
159                                        createPartyDocument(party, documentType, mimeType, blob, null, result);
160                                    } else {
161                                        addExecutionError(ExecutionErrors.MissingBlob.name());
162                                    }
163                                } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) {
164                                    var clob = form.getClob();
165
166                                    if(clob != null) {
167                                        createPartyDocument(party, documentType, mimeType, null, clob, result);
168                                    } else {
169                                        addExecutionError(ExecutionErrors.MissingClob.name());
170                                    }
171                                } else {
172                                    addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName);
173                                }
174                            } else {
175                                addExecutionError(ExecutionErrors.UnknownMimeTypeUsage.name());
176                            }
177                        } else {
178                            addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
179                        }
180                    }
181                } else {
182                    addExecutionError(ExecutionErrors.UnknownPartyTypeDocumentTypeUsageType.name());
183                }
184            } else {
185                addExecutionError(ExecutionErrors.UnknownPartyName.name(), documentTypeName);
186            }
187        } else {
188            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
189        }
190
191        return result;
192    }
193    
194}