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.printer.server.command; 018 019import com.echothree.control.user.printer.common.form.CreatePrinterForm; 020import com.echothree.control.user.printer.common.result.PrinterResultFactory; 021import com.echothree.model.control.core.server.control.EntityInstanceControl; 022import com.echothree.model.control.printer.common.workflow.PrinterStatusConstants; 023import com.echothree.model.control.printer.server.control.PrinterControl; 024import com.echothree.model.control.workflow.server.control.WorkflowControl; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.server.control.BaseSimpleCommand; 031import java.util.List; 032import javax.enterprise.context.Dependent; 033import javax.inject.Inject; 034 035@Dependent 036public class CreatePrinterCommand 037 extends BaseSimpleCommand<CreatePrinterForm> { 038 039 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 040 041 static { 042 FORM_FIELD_DEFINITIONS = List.of( 043 new FieldDefinition("PrinterName", FieldType.ENTITY_NAME, true, null, null), 044 new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null), 045 new FieldDefinition("Priority", FieldType.SIGNED_INTEGER, true, null, null), 046 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 047 ); 048 } 049 050 @Inject 051 EntityInstanceControl entityInstanceControl; 052 053 @Inject 054 PrinterControl printerControl; 055 056 @Inject 057 WorkflowControl workflowControl; 058 059 /** Creates a new instance of CreatePrinterCommand */ 060 public CreatePrinterCommand() { 061 super(null, FORM_FIELD_DEFINITIONS, false); 062 } 063 064 @Override 065 protected BaseResult execute() { 066 var result = PrinterResultFactory.getCreatePrinterResult(); 067 var printerName = form.getPrinterName(); 068 var printer = printerControl.getPrinterByName(printerName); 069 070 if(printer == null) { 071 var printerGroupName = form.getPrinterGroupName(); 072 var printerGroup = printerControl.getPrinterGroupByName(printerGroupName); 073 074 if(printerGroup != null) { 075 var priority = Integer.valueOf(form.getPriority()); 076 var description = form.getDescription(); 077 var createdBy = getPartyPK(); 078 079 printer = printerControl.createPrinter(printerName, printerGroup, priority, createdBy); 080 081 if(description != null) { 082 var language = getPreferredLanguage(); 083 084 printerControl.createPrinterDescription(printer, language, description, createdBy); 085 } 086 087 var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(printer.getPrimaryKey()); 088 workflowControl.addEntityToWorkflowUsingNames(null, PrinterStatusConstants.Workflow_PRINTER_STATUS, PrinterStatusConstants.WorkflowEntrance_NEW_PRINTER, entityInstance, null, null, 089 createdBy); 090 } else { 091 addExecutionError(ExecutionErrors.UnknownPrinterGroupName.name(), printerGroupName); 092 } 093 } else { 094 addExecutionError(ExecutionErrors.DuplicatePrinterName.name(), printerName); 095 } 096 097 if(printer != null) { 098 result.setEntityRef(printer.getPrimaryKey().getEntityRef()); 099 result.setPrinterName(printer.getLastDetail().getPrinterName()); 100 } 101 102 return result; 103 } 104 105}