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.CreatePrinterGroupForm;
020import com.echothree.model.control.core.server.control.EntityInstanceControl;
021import com.echothree.model.control.printer.common.workflow.PrinterGroupStatusConstants;
022import com.echothree.model.control.printer.server.control.PrinterControl;
023import com.echothree.model.control.uom.common.UomConstants;
024import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic;
025import com.echothree.model.control.workflow.server.control.WorkflowControl;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036import javax.enterprise.context.RequestScoped;
037
038@RequestScoped
039public class CreatePrinterGroupCommand
040        extends BaseSimpleCommand<CreatePrinterGroupForm> {
041    
042   private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046                new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("KeepPrintedJobsTime", FieldType.UNSIGNED_LONG, false, null, null),
048                new FieldDefinition("KeepPrintedJobsTimeUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
050                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
051                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
052                ));
053    }
054
055    /** Creates a new instance of CreatePrinterGroupCommand */
056    public CreatePrinterGroupCommand() {
057        super(null, FORM_FIELD_DEFINITIONS, false);
058    }
059    
060   @Override
061    protected BaseResult execute() {
062        var printerControl = Session.getModelController(PrinterControl.class);
063       var printerGroupName = form.getPrinterGroupName();
064       var printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
065        
066        if(printerGroup == null) {
067            var unitOfMeasureTypeLogic = UnitOfMeasureTypeLogic.getInstance();
068            var keepPrintedJobsTime = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, UomConstants.UnitOfMeasureKindUseType_TIME,
069                    form.getKeepPrintedJobsTime(), form.getKeepPrintedJobsTimeUnitOfMeasureTypeName(),
070                    null, ExecutionErrors.MissingRequiredKeepPrintedJobsTime.name(), null, ExecutionErrors.MissingRequiredKeepPrintedJobsTimeUnitOfMeasureTypeName.name(),
071                    null, ExecutionErrors.UnknownKeepPrintedJobsTimeUnitOfMeasureTypeName.name());
072
073            if(!hasExecutionErrors()) {
074                var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
075                var workflowControl = Session.getModelController(WorkflowControl.class);
076                var isDefault = Boolean.valueOf(form.getIsDefault());
077                var sortOrder = Integer.valueOf(form.getSortOrder());
078                var description = form.getDescription();
079                var createdBy = getPartyPK();
080
081                printerGroup = printerControl.createPrinterGroup(printerGroupName, keepPrintedJobsTime, isDefault, sortOrder, createdBy);
082
083                if(description != null) {
084                    var language = getPreferredLanguage();
085
086                    printerControl.createPrinterGroupDescription(printerGroup, language, description, createdBy);
087                }
088
089                var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(printerGroup.getPrimaryKey());
090                workflowControl.addEntityToWorkflowUsingNames(null, PrinterGroupStatusConstants.Workflow_PRINTER_GROUP_STATUS,
091                        PrinterGroupStatusConstants.WorkflowEntrance_NEW_PRINTER_GROUP, entityInstance, null, null, createdBy);
092            }
093        } else {
094            addExecutionError(ExecutionErrors.DuplicatePrinterGroupName.name(), printerGroupName);
095        }
096        
097        return null;
098    }
099    
100}