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