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.carrier.server.command;
018
019import com.echothree.control.user.carrier.common.edit.CarrierEditFactory;
020import com.echothree.control.user.carrier.common.edit.PartyCarrierAccountEdit;
021import com.echothree.control.user.carrier.common.form.EditPartyCarrierAccountForm;
022import com.echothree.control.user.carrier.common.result.CarrierResultFactory;
023import com.echothree.control.user.carrier.common.result.EditPartyCarrierAccountResult;
024import com.echothree.control.user.carrier.common.spec.PartyCarrierAccountSpec;
025import com.echothree.model.control.carrier.server.control.CarrierControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.carrier.server.entity.PartyCarrierAccount;
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.Arrays;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class EditPartyCarrierAccountCommand
048        extends BaseAbstractEditCommand<PartyCarrierAccountSpec, PartyCarrierAccountEdit, EditPartyCarrierAccountResult, PartyCarrierAccount, PartyCarrierAccount> {
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(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.PartyCarrierAccount.name(), SecurityRoles.Edit.name())
059                        )))
060                )));
061
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null)
065                ));
066        
067        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("Account", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("AlwaysUseThirdPartyBilling", FieldType.BOOLEAN, true, null, null)
070                ));
071    }
072    
073    /** Creates a new instance of EditPartyCarrierAccountCommand */
074    public EditPartyCarrierAccountCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    public EditPartyCarrierAccountResult getResult() {
080        return CarrierResultFactory.getEditPartyCarrierAccountResult();
081    }
082
083    @Override
084    public PartyCarrierAccountEdit getEdit() {
085        return CarrierEditFactory.getPartyCarrierAccountEdit();
086    }
087
088    @Override
089    public PartyCarrierAccount getEntity(EditPartyCarrierAccountResult result) {
090        var partyControl = Session.getModelController(PartyControl.class);
091        PartyCarrierAccount partyCarrierAccount = null;
092        var partyName = spec.getPartyName();
093        var party = partyControl.getPartyByName(partyName);
094
095        if(party != null) {
096            var carrierControl = Session.getModelController(CarrierControl.class);
097            var carrierName = spec.getCarrierName();
098            var carrier = carrierControl.getCarrierByName(carrierName);
099
100            if(carrier != null) {
101                var carrierParty = carrier.getParty();
102
103                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
104                    partyCarrierAccount = carrierControl.getPartyCarrierAccount(party, carrierParty);
105                } else { // EditMode.UPDATE
106                    partyCarrierAccount = carrierControl.getPartyCarrierAccountForUpdate(party, carrierParty);
107                }
108
109                if(partyCarrierAccount != null) {
110                    result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount));
111                } else {
112                    addExecutionError(ExecutionErrors.UnknownPartyCarrierAccount.name(), partyName, carrierName);
113                }
114            } else {
115                addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
116            }
117        } else {
118            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
119        }
120
121        return partyCarrierAccount;
122    }
123
124    @Override
125    public PartyCarrierAccount getLockEntity(PartyCarrierAccount partyCarrierAccount) {
126        return partyCarrierAccount;
127    }
128
129    @Override
130    public void fillInResult(EditPartyCarrierAccountResult result, PartyCarrierAccount partyCarrierAccount) {
131        var carrierControl = Session.getModelController(CarrierControl.class);
132
133        result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount));
134    }
135
136    @Override
137    public void doLock(PartyCarrierAccountEdit edit, PartyCarrierAccount partyCarrierAccount) {
138        var partyCarrierAccountDetail = partyCarrierAccount.getLastDetail();
139
140        edit.setAccount(partyCarrierAccountDetail.getAccount());
141        edit.setAlwaysUseThirdPartyBilling(partyCarrierAccountDetail.getAlwaysUseThirdPartyBilling().toString());
142    }
143
144    @Override
145    public void doUpdate(PartyCarrierAccount partyCarrierAccount) {
146        var carrierControl = Session.getModelController(CarrierControl.class);
147        var partyCarrierAccountDetailValue = carrierControl.getPartyCarrierAccountDetailValueForUpdate(partyCarrierAccount);
148
149        partyCarrierAccountDetailValue.setAccount(edit.getAccount());
150        partyCarrierAccountDetailValue.setAlwaysUseThirdPartyBilling(Boolean.valueOf(edit.getAlwaysUseThirdPartyBilling()));
151
152        carrierControl.updatePartyCarrierAccountFromValue(partyCarrierAccountDetailValue, getPartyPK());
153    }
154
155}