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