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.edit.PrinterEdit;
020import com.echothree.control.user.printer.common.edit.PrinterEditFactory;
021import com.echothree.control.user.printer.common.form.EditPrinterForm;
022import com.echothree.control.user.printer.common.result.EditPrinterResult;
023import com.echothree.control.user.printer.common.result.PrinterResultFactory;
024import com.echothree.control.user.printer.common.spec.PrinterSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.printer.server.control.PrinterControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.printer.server.entity.Printer;
030import com.echothree.model.data.printer.server.entity.PrinterGroup;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditPrinterCommand
046        extends BaseAbstractEditCommand<PrinterSpec, PrinterEdit, EditPrinterResult, Printer, Printer> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Printer.name(), SecurityRoles.Edit.name())
057                        ))
058                ));
059
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("PrinterName", FieldType.ENTITY_NAME, true, null, null)
062                );
063
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("PrinterName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("Priority", FieldType.SIGNED_INTEGER, true, null, null),
067                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
068                );
069    }
070
071    /** Creates a new instance of EditPrinterCommand */
072    public EditPrinterCommand() {
073        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
074    }
075
076    @Override
077    public EditPrinterResult getResult() {
078        return PrinterResultFactory.getEditPrinterResult();
079    }
080
081    @Override
082    public PrinterEdit getEdit() {
083        return PrinterEditFactory.getPrinterEdit();
084    }
085
086    @Override
087    public Printer getEntity(EditPrinterResult result) {
088        var printerControl = Session.getModelController(PrinterControl.class);
089        Printer printer;
090        var printerName = spec.getPrinterName();
091
092        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
093            printer = printerControl.getPrinterByName(printerName);
094        } else { // EditMode.UPDATE
095            printer = printerControl.getPrinterByNameForUpdate(printerName);
096        }
097
098        if(printer != null) {
099            result.setPrinter(printerControl.getPrinterTransfer(getUserVisit(), printer));
100        } else {
101            addExecutionError(ExecutionErrors.UnknownPrinterName.name(), printerName);
102        }
103
104        return printer;
105    }
106
107    @Override
108    public Printer getLockEntity(Printer printer) {
109        return printer;
110    }
111
112    @Override
113    public void fillInResult(EditPrinterResult result, Printer printer) {
114        var printerControl = Session.getModelController(PrinterControl.class);
115
116        result.setPrinter(printerControl.getPrinterTransfer(getUserVisit(), printer));
117    }
118
119    @Override
120    public void doLock(PrinterEdit edit, Printer printer) {
121        var printerControl = Session.getModelController(PrinterControl.class);
122        var printerDescription = printerControl.getPrinterDescription(printer, getPreferredLanguage());
123        var printerDetail = printer.getLastDetail();
124
125        edit.setPrinterName(printerDetail.getPrinterName());
126        edit.setPrinterGroupName(printerDetail.getPrinterGroup().getLastDetail().getPrinterGroupName());
127        edit.setPriority(printerDetail.getPriority().toString());
128
129        if(printerDescription != null) {
130            edit.setDescription(printerDescription.getDescription());
131        }
132    }
133
134    PrinterGroup printerGroup;
135
136    @Override
137    public void canUpdate(Printer printer) {
138        var printerControl = Session.getModelController(PrinterControl.class);
139        var printerName = edit.getPrinterName();
140        var duplicatePrinter = printerControl.getPrinterByName(printerName);
141
142        if(duplicatePrinter != null && !printer.equals(duplicatePrinter)) {
143            addExecutionError(ExecutionErrors.DuplicatePrinterName.name(), printerName);
144        } else {
145            var printerGroupName = edit.getPrinterGroupName();
146
147            printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
148
149            if(printerGroup == null) {
150                addExecutionError(ExecutionErrors.DuplicatePrinterGroupName.name(), printerGroupName);
151            }
152        }
153    }
154
155    @Override
156    public void doUpdate(Printer printer) {
157        var printerControl = Session.getModelController(PrinterControl.class);
158        var partyPK = getPartyPK();
159        var printerDetailValue = printerControl.getPrinterDetailValueForUpdate(printer);
160        var printerDescription = printerControl.getPrinterDescriptionForUpdate(printer, getPreferredLanguage());
161        var description = edit.getDescription();
162
163        printerDetailValue.setPrinterName(edit.getPrinterName());
164        printerDetailValue.setPrinterGroupPK(printerGroup.getPrimaryKey());
165        printerDetailValue.setPriority(Integer.valueOf(edit.getPriority()));
166
167        printerControl.updatePrinterFromValue(printerDetailValue, partyPK);
168
169        if(printerDescription == null && description != null) {
170            printerControl.createPrinterDescription(printer, getPreferredLanguage(), description, partyPK);
171        } else if(printerDescription != null && description == null) {
172            printerControl.deletePrinterDescription(printerDescription, partyPK);
173        } else if(printerDescription != null && description != null) {
174            var printerDescriptionValue = printerControl.getPrinterDescriptionValue(printerDescription);
175
176            printerDescriptionValue.setDescription(description);
177            printerControl.updatePrinterDescriptionFromValue(printerDescriptionValue, partyPK);
178        }
179    }
180
181}