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