001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.edit.DocumentEditFactory; 020import com.echothree.control.user.document.common.edit.PartyDocumentEdit; 021import com.echothree.control.user.document.common.form.EditPartyDocumentForm; 022import com.echothree.control.user.document.common.result.DocumentResultFactory; 023import com.echothree.control.user.document.common.result.EditPartyDocumentResult; 024import com.echothree.control.user.document.common.spec.DocumentSpec; 025import com.echothree.model.control.core.common.EntityAttributeTypes; 026import com.echothree.model.control.document.server.control.DocumentControl; 027import com.echothree.model.control.document.server.logic.DocumentLogic; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.data.core.server.entity.MimeType; 032import com.echothree.model.data.core.server.entity.MimeTypeUsage; 033import com.echothree.model.data.core.server.entity.MimeTypeUsageType; 034import com.echothree.model.data.document.server.entity.Document; 035import com.echothree.model.data.document.server.entity.DocumentBlob; 036import com.echothree.model.data.document.server.entity.DocumentClob; 037import com.echothree.model.data.document.server.entity.DocumentDescription; 038import com.echothree.model.data.document.server.entity.PartyDocument; 039import com.echothree.model.data.document.server.value.DocumentBlobValue; 040import com.echothree.model.data.document.server.value.DocumentClobValue; 041import com.echothree.model.data.document.server.value.DocumentDescriptionValue; 042import com.echothree.model.data.document.server.value.DocumentDetailValue; 043import com.echothree.model.data.document.server.value.PartyDocumentValue; 044import com.echothree.model.data.user.common.pk.UserVisitPK; 045import com.echothree.util.common.message.ExecutionErrors; 046import com.echothree.util.common.validation.FieldDefinition; 047import com.echothree.util.common.validation.FieldType; 048import com.echothree.util.common.command.EditMode; 049import com.echothree.util.common.persistence.type.ByteArray; 050import com.echothree.util.server.control.BaseAbstractEditCommand; 051import com.echothree.util.server.control.CommandSecurityDefinition; 052import com.echothree.util.server.control.PartyTypeDefinition; 053import com.echothree.util.server.control.SecurityRoleDefinition; 054import com.echothree.util.server.persistence.Session; 055import java.util.Arrays; 056import java.util.Collections; 057import java.util.List; 058 059public class EditPartyDocumentCommand 060 extends BaseAbstractEditCommand<DocumentSpec, PartyDocumentEdit, EditPartyDocumentResult, PartyDocument, Document> { 061 062 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 063 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 064 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 065 066 static { 067 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 068 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 069 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 070 new SecurityRoleDefinition(SecurityRoleGroups.PartyDocument.name(), SecurityRoles.Edit.name()) 071 ))) 072 ))); 073 074 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 075 new FieldDefinition("DocumentName", FieldType.ENTITY_NAME, true, null, null) 076 )); 077 078 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 079 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null), 080 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 081 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 082 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L), 083 new FieldDefinition("Clob", FieldType.STRING, false, 1L, null) 084 )); 085 } 086 087 /** Creates a new instance of EditPartyDocumentCommand */ 088 public EditPartyDocumentCommand(UserVisitPK userVisitPK, EditPartyDocumentForm form) { 089 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 090 } 091 092 @Override 093 public EditPartyDocumentResult getResult() { 094 return DocumentResultFactory.getEditPartyDocumentResult(); 095 } 096 097 @Override 098 public PartyDocumentEdit getEdit() { 099 return DocumentEditFactory.getPartyDocumentEdit(); 100 } 101 102 @Override 103 public PartyDocument getEntity(EditPartyDocumentResult result) { 104 var documentControl = Session.getModelController(DocumentControl.class); 105 PartyDocument partyDocument = null; 106 String documentName = spec.getDocumentName(); 107 Document document = documentControl.getDocumentByName(documentName); 108 109 if(document != null) { 110 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 111 partyDocument = documentControl.getPartyDocument(document); 112 } else { // EditMode.UPDATE 113 partyDocument = documentControl.getPartyDocumentForUpdate(document); 114 } 115 116 if(partyDocument != null) { 117 result.setPartyDocument(documentControl.getPartyDocumentTransfer(getUserVisit(), partyDocument)); 118 } else { 119 addExecutionError(ExecutionErrors.UnknownPartyDocument.name(), documentName); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownDocumentName.name(), documentName); 123 } 124 125 return partyDocument; 126 } 127 128 @Override 129 public Document getLockEntity(PartyDocument partyDocument) { 130 return partyDocument.getDocument(); 131 } 132 133 @Override 134 public void fillInResult(EditPartyDocumentResult result, PartyDocument partyDocument) { 135 var documentControl = Session.getModelController(DocumentControl.class); 136 137 result.setPartyDocument(documentControl.getPartyDocumentTransfer(getUserVisit(), partyDocument)); 138 } 139 140 MimeType mimeType; 141 142 @Override 143 public void doLock(PartyDocumentEdit edit, PartyDocument partyDocument) { 144 var documentControl = Session.getModelController(DocumentControl.class); 145 Document document = partyDocument.getDocument(); 146 DocumentDescription documentDescription = documentControl.getDocumentDescription(document, getPreferredLanguage()); 147 148 mimeType = document.getLastDetail().getMimeType(); 149 150 edit.setMimeTypeName(mimeType.getLastDetail().getMimeTypeName()); 151 edit.setIsDefault(partyDocument.getIsDefault().toString()); 152 edit.setSortOrder(partyDocument.getSortOrder().toString()); 153 154 if(mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.CLOB.name())) { 155 DocumentClob documentClob = documentControl.getDocumentClob(document); 156 157 edit.setClob(documentClob.getClob()); 158 } 159 160 if(documentDescription != null) { 161 edit.setDescription(documentDescription.getDescription()); 162 } 163 } 164 165 @Override 166 public void canUpdate(PartyDocument partyDocument) { 167 var coreControl = getCoreControl(); 168 String mimeTypeName = edit.getMimeTypeName(); 169 170 mimeType = coreControl.getMimeTypeByName(mimeTypeName); 171 172 if(mimeType != null) { 173 MimeTypeUsageType mimeTypeUsageType = partyDocument.getDocument().getLastDetail().getDocumentType().getLastDetail().getMimeTypeUsageType(); 174 MimeTypeUsage mimeTypeUsage = mimeTypeUsageType == null ? null : coreControl.getMimeTypeUsage(mimeType, mimeTypeUsageType); 175 176 if(mimeTypeUsageType == null || mimeTypeUsage != null) { 177 String entityAttributeTypeName = mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName(); 178 179 if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) { 180 ByteArray blob = edit.getBlob(); 181 182 if(blob == null) { 183 addExecutionError(ExecutionErrors.MissingBlob.name()); 184 } 185 } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) { 186 String clob = edit.getClob(); 187 188 if(clob == null) { 189 addExecutionError(ExecutionErrors.MissingClob.name()); 190 } 191 } else { 192 addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName); 193 } 194 } else { 195 addExecutionError(ExecutionErrors.UnknownMimeTypeUsage.name()); 196 } 197 } else { 198 addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName); 199 } 200 } 201 202 @Override 203 public void doUpdate(PartyDocument partyDocument) { 204 var documentControl = Session.getModelController(DocumentControl.class); 205 PartyDocumentValue partyDocumentValue = documentControl.getPartyDocumentValueForUpdate(partyDocument); 206 Document document = partyDocument.getDocument(); 207 DocumentDetailValue documentDetailValue = documentControl.getDocumentDetailValueForUpdate(document); 208 ByteArray blob = edit.getBlob(); 209 String clob = edit.getClob(); 210 Integer pages = DocumentLogic.getInstance().getPages(mimeType, blob, clob); 211 var partyPK = getPartyPK(); 212 213 partyDocumentValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 214 partyDocumentValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 215 216 documentControl.updatePartyDocumentFromValue(partyDocumentValue, partyPK); 217 218 documentDetailValue.setMimeTypePK(mimeType.getPrimaryKey()); 219 documentDetailValue.setPages(pages); 220 221 documentControl.updateDocumentFromValue(documentDetailValue, partyPK); 222 223 doLobUpdate(document, blob, clob); 224 doDescriptionUpdate(document); 225 } 226 227 private void doLobUpdate(Document document, ByteArray blob, String clob) { 228 var documentControl = Session.getModelController(DocumentControl.class); 229 DocumentBlob documentBlob = null; 230 DocumentClob documentClob = null; 231 String oldEntityAttributeTypeName = document.getLastDetail().getMimeType().getLastDetail().getEntityAttributeType().getEntityAttributeTypeName(); 232 String entityAttributeTypeName = mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName(); 233 var partyPK = getPartyPK(); 234 235 if(oldEntityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) { 236 documentBlob = documentControl.getDocumentBlobForUpdate(document); 237 } else if(oldEntityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) { 238 documentClob = documentControl.getDocumentClobForUpdate(document); 239 } 240 241 if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) { 242 if(documentClob != null) { 243 documentControl.deleteDocumentClob(documentClob, partyPK); 244 documentControl.createDocumentBlob(document, blob, partyPK); 245 } else { 246 DocumentBlobValue documentBlobValue = documentControl.getDocumentBlobValue(documentBlob); 247 documentBlobValue.setBlob(blob); 248 documentControl.updateDocumentBlobFromValue(documentBlobValue, partyPK); 249 } 250 } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) { 251 if(documentBlob != null) { 252 documentControl.deleteDocumentBlob(documentBlob, partyPK); 253 documentControl.createDocumentClob(document, clob, partyPK); 254 } else { 255 DocumentClobValue documentClobValue = documentControl.getDocumentClobValue(documentClob); 256 documentClobValue.setClob(clob); 257 documentControl.updateDocumentClobFromValue(documentClobValue, partyPK); 258 } 259 } 260 } 261 262 private void doDescriptionUpdate(Document document) { 263 var documentControl = Session.getModelController(DocumentControl.class); 264 DocumentDescription documentDescription = documentControl.getDocumentDescriptionForUpdate(document, getPreferredLanguage()); 265 String description = edit.getDescription(); 266 var partyPK = getPartyPK(); 267 268 if(documentDescription == null && description != null) { 269 documentControl.createDocumentDescription(document, getPreferredLanguage(), description, partyPK); 270 } else { 271 if(documentDescription != null && description == null) { 272 documentControl.deleteDocumentDescription(documentDescription, partyPK); 273 } else { 274 if(documentDescription != null && description != null) { 275 DocumentDescriptionValue documentDescriptionValue = documentControl.getDocumentDescriptionValue(documentDescription); 276 documentDescriptionValue.setDescription(description); 277 documentControl.updateDocumentDescriptionFromValue(documentDescriptionValue, partyPK); 278 } 279 } 280 } 281 } 282 283}