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