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.printer.server.command; 018 019import com.echothree.control.user.printer.common.form.CreatePrinterGroupJobForm; 020import com.echothree.control.user.printer.common.result.CreatePrinterGroupJobResult; 021import com.echothree.control.user.printer.common.result.PrinterResultFactory; 022import com.echothree.model.control.core.common.EntityAttributeTypes; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.printer.server.control.PrinterControl; 025import com.echothree.model.control.printer.server.logic.PrinterGroupJobLogic; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.data.core.server.entity.EntityAttributeType; 029import com.echothree.model.data.core.server.entity.MimeType; 030import com.echothree.model.data.printer.server.entity.PrinterGroup; 031import com.echothree.model.data.printer.server.entity.PrinterGroupJob; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.common.command.BaseResult; 037import com.echothree.util.common.persistence.type.ByteArray; 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 com.echothree.util.server.persistence.Session; 043import java.util.Arrays; 044import java.util.Collections; 045import java.util.List; 046 047public class CreatePrinterGroupJobCommand 048 extends BaseSimpleCommand<CreatePrinterGroupJobForm> { 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(Collections.unmodifiableList(Arrays.asList( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.PrinterGroupJob.name(), SecurityRoles.Create.name()) 058 ))) 059 ))); 060 061 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null), 064 new FieldDefinition("Copies", FieldType.SIGNED_INTEGER, true, null, null), 065 new FieldDefinition("Priority", FieldType.UNSIGNED_INTEGER, true, 1L, 100L), 066 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L), 067 new FieldDefinition("Clob", FieldType.STRING, false, 1L, null) 068 )); 069 } 070 071 /** Creates a new instance of CreatePrinterGroupJobCommand */ 072 public CreatePrinterGroupJobCommand(UserVisitPK userVisitPK, CreatePrinterGroupJobForm form) { 073 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 074 } 075 076 private void createPrinterGroupJob(final PrinterGroup printerGroup, final MimeType mimeType, final ByteArray blob, final String clob, 077 final CreatePrinterGroupJobResult result) { 078 Integer copies = Integer.valueOf(form.getCopies()); 079 Integer priority = Integer.valueOf(form.getPriority()); 080 var description = form.getDescription(); 081 082 PrinterGroupJob printerGroupJob = PrinterGroupJobLogic.getInstance().createPrinterGroupJob(this, printerGroup, copies, priority, mimeType, 083 getPreferredLanguage(), description, blob, clob, getPartyPK()); 084 085 if(!hasExecutionErrors()) { 086 result.setEntityRef(printerGroupJob.getPrimaryKey().getEntityRef()); 087 result.setPrinterGroupJobName(printerGroupJob.getLastDetail().getPrinterGroupJobName()); 088 } 089 } 090 091 @Override 092 protected BaseResult execute() { 093 var printerControl = Session.getModelController(PrinterControl.class); 094 CreatePrinterGroupJobResult result = PrinterResultFactory.getCreatePrinterGroupJobResult(); 095 String printerGroupName = form.getPrinterGroupName(); 096 PrinterGroup printerGroup = printerControl.getPrinterGroupByName(printerGroupName); 097 098 if(printerGroup != null) { 099 var coreControl = getCoreControl(); 100 String mimeTypeName = form.getMimeTypeName(); 101 MimeType mimeType = coreControl.getMimeTypeByName(mimeTypeName); 102 103 if(mimeType != null) { 104 EntityAttributeType entityAttributeType = mimeType.getLastDetail().getEntityAttributeType(); 105 String entityAttributeTypeName = entityAttributeType.getEntityAttributeTypeName(); 106 107 if(entityAttributeTypeName.equals(EntityAttributeTypes.BLOB.name())) { 108 ByteArray blob = form.getBlob(); 109 110 if(blob != null) { 111 createPrinterGroupJob(printerGroup, mimeType, blob, null, result); 112 } else { 113 addExecutionError(ExecutionErrors.MissingBlob.name()); 114 } 115 } else if(entityAttributeTypeName.equals(EntityAttributeTypes.CLOB.name())) { 116 String clob = form.getClob(); 117 118 if(clob != null) { 119 createPrinterGroupJob(printerGroup, mimeType, null, clob, result); 120 } else { 121 addExecutionError(ExecutionErrors.MissingClob.name()); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownEntityAttributeTypeName.name(), entityAttributeTypeName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName); 131 } 132 133 return result; 134 } 135 136}