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.core.server.command;
018
019import com.echothree.control.user.core.common.edit.CoreEditFactory;
020import com.echothree.control.user.core.common.edit.FontStyleDescriptionEdit;
021import com.echothree.control.user.core.common.form.EditFontStyleDescriptionForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditFontStyleDescriptionResult;
024import com.echothree.control.user.core.common.spec.FontStyleDescriptionSpec;
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.core.server.entity.FontStyle;
030import com.echothree.model.data.core.server.entity.FontStyleDescription;
031import com.echothree.model.data.core.server.value.FontStyleDescriptionValue;
032import com.echothree.model.data.party.server.entity.Language;
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.Arrays;
044import java.util.Collections;
045import java.util.List;
046
047public class EditFontStyleDescriptionCommand
048        extends BaseAbstractEditCommand<FontStyleDescriptionSpec, FontStyleDescriptionEdit, EditFontStyleDescriptionResult, FontStyleDescription, FontStyle> {
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.FontStyle.name(), SecurityRoles.Description.name())
059                        )))
060                )));
061        
062        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("FontStyleName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
065                ));
066        
067        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069                ));
070    }
071    
072    /** Creates a new instance of EditFontStyleDescriptionCommand */
073    public EditFontStyleDescriptionCommand(UserVisitPK userVisitPK, EditFontStyleDescriptionForm form) {
074        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    public EditFontStyleDescriptionResult getResult() {
079        return CoreResultFactory.getEditFontStyleDescriptionResult();
080    }
081
082    @Override
083    public FontStyleDescriptionEdit getEdit() {
084        return CoreEditFactory.getFontStyleDescriptionEdit();
085    }
086
087    @Override
088    public FontStyleDescription getEntity(EditFontStyleDescriptionResult result) {
089        var coreControl = getCoreControl();
090        FontStyleDescription fontStyleDescription = null;
091        String fontStyleName = spec.getFontStyleName();
092        FontStyle fontStyle = coreControl.getFontStyleByName(fontStyleName);
093
094        if(fontStyle != null) {
095            var partyControl = Session.getModelController(PartyControl.class);
096            String languageIsoName = spec.getLanguageIsoName();
097            Language language = partyControl.getLanguageByIsoName(languageIsoName);
098
099            if(language != null) {
100                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
101                    fontStyleDescription = coreControl.getFontStyleDescription(fontStyle, language);
102                } else { // EditMode.UPDATE
103                    fontStyleDescription = coreControl.getFontStyleDescriptionForUpdate(fontStyle, language);
104                }
105
106                if(fontStyleDescription == null) {
107                    addExecutionError(ExecutionErrors.UnknownFontStyleDescription.name(), fontStyleName, languageIsoName);
108                }
109            } else {
110                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
111            }
112        } else {
113            addExecutionError(ExecutionErrors.UnknownFontStyleName.name(), fontStyleName);
114        }
115
116        return fontStyleDescription;
117    }
118
119    @Override
120    public FontStyle getLockEntity(FontStyleDescription fontStyleDescription) {
121        return fontStyleDescription.getFontStyle();
122    }
123
124    @Override
125    public void fillInResult(EditFontStyleDescriptionResult result, FontStyleDescription fontStyleDescription) {
126        var coreControl = getCoreControl();
127
128        result.setFontStyleDescription(coreControl.getFontStyleDescriptionTransfer(getUserVisit(), fontStyleDescription));
129    }
130
131    @Override
132    public void doLock(FontStyleDescriptionEdit edit, FontStyleDescription fontStyleDescription) {
133        edit.setDescription(fontStyleDescription.getDescription());
134    }
135
136    @Override
137    public void doUpdate(FontStyleDescription fontStyleDescription) {
138        var coreControl = getCoreControl();
139        FontStyleDescriptionValue fontStyleDescriptionValue = coreControl.getFontStyleDescriptionValue(fontStyleDescription);
140        fontStyleDescriptionValue.setDescription(edit.getDescription());
141
142        coreControl.updateFontStyleDescriptionFromValue(fontStyleDescriptionValue, getPartyPK());
143    }
144    
145}