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.party.server.command;
018
019import com.echothree.control.user.party.common.edit.BirthdayFormatDescriptionEdit;
020import com.echothree.control.user.party.common.edit.PartyEditFactory;
021import com.echothree.control.user.party.common.form.EditBirthdayFormatDescriptionForm;
022import com.echothree.control.user.party.common.result.EditBirthdayFormatDescriptionResult;
023import com.echothree.control.user.party.common.result.PartyResultFactory;
024import com.echothree.control.user.party.common.spec.BirthdayFormatDescriptionSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.party.server.entity.BirthdayFormat;
030import com.echothree.model.data.party.server.entity.BirthdayFormatDescription;
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 java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditBirthdayFormatDescriptionCommand
046        extends BaseAbstractEditCommand<BirthdayFormatDescriptionSpec, BirthdayFormatDescriptionEdit, EditBirthdayFormatDescriptionResult, BirthdayFormatDescription, BirthdayFormat> {
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.BirthdayFormat.name(), SecurityRoles.Description.name())
057                ))
058        ));
059
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("BirthdayFormatName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
063        );
064
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
067        );
068    }
069
070    @Inject
071    PartyControl partyControl;
072
073    /** Creates a new instance of EditBirthdayFormatDescriptionCommand */
074    public EditBirthdayFormatDescriptionCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Override
079    public EditBirthdayFormatDescriptionResult getResult() {
080        return PartyResultFactory.getEditBirthdayFormatDescriptionResult();
081    }
082
083    @Override
084    public BirthdayFormatDescriptionEdit getEdit() {
085        return PartyEditFactory.getBirthdayFormatDescriptionEdit();
086    }
087
088    @Override
089    public BirthdayFormatDescription getEntity(EditBirthdayFormatDescriptionResult result) {
090        BirthdayFormatDescription birthdayFormatDescription = null;
091        var birthdayFormatName = spec.getBirthdayFormatName();
092        var birthdayFormat = partyControl.getBirthdayFormatByName(birthdayFormatName);
093
094        if(birthdayFormat != null) {
095            var languageIsoName = spec.getLanguageIsoName();
096            var language = partyControl.getLanguageByIsoName(languageIsoName);
097
098            if(language != null) {
099                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
100                    birthdayFormatDescription = partyControl.getBirthdayFormatDescription(birthdayFormat, language);
101                } else { // EditMode.UPDATE
102                    birthdayFormatDescription = partyControl.getBirthdayFormatDescriptionForUpdate(birthdayFormat, language);
103                }
104
105                if(birthdayFormatDescription == null) {
106                    addExecutionError(ExecutionErrors.UnknownBirthdayFormatDescription.name(), birthdayFormatName, languageIsoName);
107                }
108            } else {
109                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
110            }
111        } else {
112            addExecutionError(ExecutionErrors.UnknownBirthdayFormatName.name(), birthdayFormatName);
113        }
114
115        return birthdayFormatDescription;
116    }
117
118    @Override
119    public BirthdayFormat getLockEntity(BirthdayFormatDescription birthdayFormatDescription) {
120        return birthdayFormatDescription.getBirthdayFormat();
121    }
122
123    @Override
124    public void fillInResult(EditBirthdayFormatDescriptionResult result, BirthdayFormatDescription birthdayFormatDescription) {
125        result.setBirthdayFormatDescription(partyControl.getBirthdayFormatDescriptionTransfer(getUserVisit(), birthdayFormatDescription));
126    }
127
128    @Override
129    public void doLock(BirthdayFormatDescriptionEdit edit, BirthdayFormatDescription birthdayFormatDescription) {
130        edit.setDescription(birthdayFormatDescription.getDescription());
131    }
132
133    @Override
134    public void doUpdate(BirthdayFormatDescription birthdayFormatDescription) {
135        var birthdayFormatDescriptionValue = partyControl.getBirthdayFormatDescriptionValue(birthdayFormatDescription);
136        birthdayFormatDescriptionValue.setDescription(edit.getDescription());
137
138        partyControl.updateBirthdayFormatDescriptionFromValue(birthdayFormatDescriptionValue, getPartyPK());
139    }
140
141}