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