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.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.Carrier;
031import com.echothree.model.data.carrier.server.entity.PartyCarrierAccount;
032import com.echothree.model.data.carrier.server.entity.PartyCarrierAccountDetail;
033import com.echothree.model.data.carrier.server.value.PartyCarrierAccountDetailValue;
034import com.echothree.model.data.party.server.entity.Party;
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 EditPartyCarrierAccountCommand
050        extends BaseAbstractEditCommand<PartyCarrierAccountSpec, PartyCarrierAccountEdit, EditPartyCarrierAccountResult, PartyCarrierAccount, PartyCarrierAccount> {
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.PartyCarrierAccount.name(), SecurityRoles.Edit.name())
061                        )))
062                )));
063
064        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068        
069        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
070                new FieldDefinition("Account", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("AlwaysUseThirdPartyBilling", FieldType.BOOLEAN, true, null, null)
072                ));
073    }
074    
075    /** Creates a new instance of EditPartyCarrierAccountCommand */
076    public EditPartyCarrierAccountCommand(UserVisitPK userVisitPK, EditPartyCarrierAccountForm form) {
077        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079    
080    @Override
081    public EditPartyCarrierAccountResult getResult() {
082        return CarrierResultFactory.getEditPartyCarrierAccountResult();
083    }
084
085    @Override
086    public PartyCarrierAccountEdit getEdit() {
087        return CarrierEditFactory.getPartyCarrierAccountEdit();
088    }
089
090    @Override
091    public PartyCarrierAccount getEntity(EditPartyCarrierAccountResult result) {
092        var partyControl = Session.getModelController(PartyControl.class);
093        PartyCarrierAccount partyCarrierAccount = null;
094        String partyName = spec.getPartyName();
095        Party party = partyControl.getPartyByName(partyName);
096
097        if(party != null) {
098            var carrierControl = Session.getModelController(CarrierControl.class);
099            String carrierName = spec.getCarrierName();
100            Carrier carrier = carrierControl.getCarrierByName(carrierName);
101
102            if(carrier != null) {
103                Party carrierParty = carrier.getParty();
104
105                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
106                    partyCarrierAccount = carrierControl.getPartyCarrierAccount(party, carrierParty);
107                } else { // EditMode.UPDATE
108                    partyCarrierAccount = carrierControl.getPartyCarrierAccountForUpdate(party, carrierParty);
109                }
110
111                if(partyCarrierAccount != null) {
112                    result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount));
113                } else {
114                    addExecutionError(ExecutionErrors.UnknownPartyCarrierAccount.name(), partyName, carrierName);
115                }
116            } else {
117                addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName);
118            }
119        } else {
120            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
121        }
122
123        return partyCarrierAccount;
124    }
125
126    @Override
127    public PartyCarrierAccount getLockEntity(PartyCarrierAccount partyCarrierAccount) {
128        return partyCarrierAccount;
129    }
130
131    @Override
132    public void fillInResult(EditPartyCarrierAccountResult result, PartyCarrierAccount partyCarrierAccount) {
133        var carrierControl = Session.getModelController(CarrierControl.class);
134
135        result.setPartyCarrierAccount(carrierControl.getPartyCarrierAccountTransfer(getUserVisit(), partyCarrierAccount));
136    }
137
138    @Override
139    public void doLock(PartyCarrierAccountEdit edit, PartyCarrierAccount partyCarrierAccount) {
140        PartyCarrierAccountDetail partyCarrierAccountDetail = partyCarrierAccount.getLastDetail();
141
142        edit.setAccount(partyCarrierAccountDetail.getAccount());
143        edit.setAlwaysUseThirdPartyBilling(partyCarrierAccountDetail.getAlwaysUseThirdPartyBilling().toString());
144    }
145
146    @Override
147    public void doUpdate(PartyCarrierAccount partyCarrierAccount) {
148        var carrierControl = Session.getModelController(CarrierControl.class);
149        PartyCarrierAccountDetailValue partyCarrierAccountDetailValue = carrierControl.getPartyCarrierAccountDetailValueForUpdate(partyCarrierAccount);
150
151        partyCarrierAccountDetailValue.setAccount(edit.getAccount());
152        partyCarrierAccountDetailValue.setAlwaysUseThirdPartyBilling(Boolean.valueOf(edit.getAlwaysUseThirdPartyBilling()));
153
154        carrierControl.updatePartyCarrierAccountFromValue(partyCarrierAccountDetailValue, getPartyPK());
155    }
156
157}