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