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.PartyPrinterGroupUseEdit;
020import com.echothree.control.user.printer.common.edit.PrinterEditFactory;
021import com.echothree.control.user.printer.common.form.EditPartyPrinterGroupUseForm;
022import com.echothree.control.user.printer.common.result.EditPartyPrinterGroupUseResult;
023import com.echothree.control.user.printer.common.result.PrinterResultFactory;
024import com.echothree.control.user.printer.common.spec.PartyPrinterGroupUseSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.printer.server.control.PrinterControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.party.server.entity.Party;
031import com.echothree.model.data.printer.server.entity.PartyPrinterGroupUse;
032import com.echothree.model.data.printer.server.entity.PrinterGroup;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.common.command.EditMode;
038import com.echothree.util.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import com.echothree.util.server.persistence.Session;
043import java.util.List;
044import javax.enterprise.context.Dependent;
045
046@Dependent
047public class EditPartyPrinterGroupUseCommand
048        extends BaseAbstractEditCommand<PartyPrinterGroupUseSpec, PartyPrinterGroupUseEdit, EditPartyPrinterGroupUseResult, PartyPrinterGroupUse, Party> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.PartyPrinterGroupUse.name(), SecurityRoles.Edit.name())
059                        ))
060                ));
061
062        SPEC_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("PrinterGroupUseTypeName", FieldType.ENTITY_NAME, true, null, null)
065                );
066
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("PrinterGroupName", FieldType.ENTITY_NAME, true, null, null)
069                );
070    }
071    
072    /** Creates a new instance of EditPartyPrinterGroupUseCommand */
073    public EditPartyPrinterGroupUseCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    public EditPartyPrinterGroupUseResult getResult() {
079        return PrinterResultFactory.getEditPartyPrinterGroupUseResult();
080    }
081
082    @Override
083    public PartyPrinterGroupUseEdit getEdit() {
084        return PrinterEditFactory.getPartyPrinterGroupUseEdit();
085    }
086
087    @Override
088    public PartyPrinterGroupUse getEntity(EditPartyPrinterGroupUseResult result) {
089        PartyPrinterGroupUse partyPrinterGroupUse = null;
090        var partyName = spec.getPartyName();
091        Party party;
092
093        if(partyName != null) {
094            var partyControl = Session.getModelController(PartyControl.class);
095
096            party = partyControl.getPartyByName(partyName);
097            
098            if(party == null) {
099                addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
100            }
101        } else {
102            party = getParty();
103        }
104
105        if(!hasExecutionErrors()) {
106            var printerControl = Session.getModelController(PrinterControl.class);
107            var printerGroupUseTypeName = spec.getPrinterGroupUseTypeName();
108            var printerGroupUseType = printerControl.getPrinterGroupUseTypeByName(printerGroupUseTypeName);
109
110            if(printerGroupUseType != null) {
111                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
112                    partyPrinterGroupUse = printerControl.getPartyPrinterGroupUse(party, printerGroupUseType);
113                } else { // EditMode.UPDATE
114                    partyPrinterGroupUse = printerControl.getPartyPrinterGroupUseForUpdate(party, printerGroupUseType);
115                }
116
117                if(partyPrinterGroupUse == null) {
118                    addExecutionError(ExecutionErrors.UnknownPartyPrinterGroupUse.name(), party.getLastDetail().getPartyName(), printerGroupUseTypeName);
119                }
120            } else {
121                addExecutionError(ExecutionErrors.UnknownPrinterGroupUseTypeName.name(), printerGroupUseTypeName);
122            }
123        }
124
125        return partyPrinterGroupUse;
126    }
127
128    @Override
129    public Party getLockEntity(PartyPrinterGroupUse partyPrinterGroupUse) {
130        return partyPrinterGroupUse.getParty();
131    }
132
133    @Override
134    public void fillInResult(EditPartyPrinterGroupUseResult result, PartyPrinterGroupUse partyPrinterGroupUse) {
135        var printerControl = Session.getModelController(PrinterControl.class);
136
137        result.setPartyPrinterGroupUse(printerControl.getPartyPrinterGroupUseTransfer(getUserVisit(), partyPrinterGroupUse));
138    }
139
140    @Override
141    public void doLock(PartyPrinterGroupUseEdit edit, PartyPrinterGroupUse partyPrinterGroupUse) {
142        edit.setPrinterGroupName(partyPrinterGroupUse.getPrinterGroup().getLastDetail().getPrinterGroupName());
143    }
144
145    PrinterGroup printerGroup;
146
147    @Override
148    public void canUpdate(PartyPrinterGroupUse partyPrinterGroupUse) {
149        var printerControl = Session.getModelController(PrinterControl.class);
150        var printerGroupName = edit.getPrinterGroupName();
151
152        printerGroup = printerControl.getPrinterGroupByName(printerGroupName);
153
154        if(printerGroup == null) {
155            addExecutionError(ExecutionErrors.DuplicatePrinterGroupName.name(), printerGroupName);
156        }
157    }
158
159    @Override
160    public void doUpdate(PartyPrinterGroupUse partyPrinterGroupUse) {
161        var printerControl = Session.getModelController(PrinterControl.class);
162        var partyPrinterGroupUseValue = printerControl.getPartyPrinterGroupUseValue(partyPrinterGroupUse);
163
164        partyPrinterGroupUseValue.setPrinterGroupPK(printerGroup.getPrimaryKey());
165
166        printerControl.updatePartyPrinterGroupUseFromValue(partyPrinterGroupUseValue, getPartyPK());
167    }
168    
169}