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.core.server.command;
018
019import com.echothree.control.user.core.common.edit.AppearanceEdit;
020import com.echothree.control.user.core.common.edit.CoreEditFactory;
021import com.echothree.control.user.core.common.form.EditAppearanceForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.result.EditAppearanceResult;
024import com.echothree.control.user.core.common.spec.AppearanceSpec;
025import com.echothree.model.control.core.server.control.AppearanceControl;
026import com.echothree.model.control.core.server.control.ColorControl;
027import com.echothree.model.control.core.server.logic.FontLogic;
028import com.echothree.model.control.party.common.PartyTypes;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.data.core.server.entity.Appearance;
032import com.echothree.model.data.core.server.entity.Color;
033import com.echothree.model.data.core.server.entity.FontStyle;
034import com.echothree.model.data.core.server.entity.FontWeight;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.common.message.ExecutionErrors;
038import com.echothree.util.common.validation.FieldDefinition;
039import com.echothree.util.common.validation.FieldType;
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;
048import javax.enterprise.context.RequestScoped;
049
050@RequestScoped
051public class EditAppearanceCommand
052        extends BaseAbstractEditCommand<AppearanceSpec, AppearanceEdit, EditAppearanceResult, Appearance, Appearance> {
053    
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
056    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
057    
058    static {
059        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
060                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
062                        new SecurityRoleDefinition(SecurityRoleGroups.Appearance.name(), SecurityRoles.Edit.name())
063                        )))
064                )));
065        
066        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
067                new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, true, null, null)
068                ));
069        
070        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
071                new FieldDefinition("AppearanceName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("TextColorName", FieldType.ENTITY_NAME, false, null, null),
073                new FieldDefinition("BackgroundColorName", FieldType.ENTITY_NAME, false, null, null),
074                new FieldDefinition("FontStyleName", FieldType.ENTITY_NAME, false, null, null),
075                new FieldDefinition("FontWeightName", FieldType.ENTITY_NAME, false, null, null),
076                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
077                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
078                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
079                ));
080    }
081    
082    /** Creates a new instance of EditAppearanceCommand */
083    public EditAppearanceCommand() {
084        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086    
087    @Override
088    public EditAppearanceResult getResult() {
089        return CoreResultFactory.getEditAppearanceResult();
090    }
091
092    @Override
093    public AppearanceEdit getEdit() {
094        return CoreEditFactory.getAppearanceEdit();
095    }
096
097    @Override
098    public Appearance getEntity(EditAppearanceResult result) {
099        var appearanceControl = Session.getModelController(AppearanceControl.class);
100        Appearance appearance;
101        var appearanceName = spec.getAppearanceName();
102
103        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
104            appearance = appearanceControl.getAppearanceByName(appearanceName);
105        } else { // EditMode.UPDATE
106            appearance = appearanceControl.getAppearanceByNameForUpdate(appearanceName);
107        }
108
109        if(appearance == null) {
110            addExecutionError(ExecutionErrors.UnknownAppearanceName.name(), appearanceName);
111        }
112
113        return appearance;
114    }
115
116    @Override
117    public Appearance getLockEntity(Appearance appearance) {
118        return appearance;
119    }
120
121    @Override
122    public void fillInResult(EditAppearanceResult result, Appearance appearance) {
123        var appearanceControl = Session.getModelController(AppearanceControl.class);
124
125        result.setAppearance(appearanceControl.getAppearanceTransfer(getUserVisit(), appearance));
126    }
127
128    Color textColor;
129    Color backgroundColor;
130    FontStyle fontStyle;
131    FontWeight fontWeight;
132
133    @Override
134    public void doLock(AppearanceEdit edit, Appearance appearance) {
135        var appearanceControl = Session.getModelController(AppearanceControl.class);
136        var appearanceDescription = appearanceControl.getAppearanceDescription(appearance, getPreferredLanguage());
137        var appearanceDetail = appearance.getLastDetail();
138
139        textColor = appearanceDetail.getTextColor();
140        backgroundColor = appearanceDetail.getBackgroundColor();
141        fontStyle = appearanceDetail.getFontStyle();
142        fontWeight = appearanceDetail.getFontWeight();
143        
144        edit.setAppearanceName(appearanceDetail.getAppearanceName());
145        edit.setTextColorName(textColor == null ? null : textColor.getLastDetail().getColorName());
146        edit.setBackgroundColorName(backgroundColor == null ? null : backgroundColor.getLastDetail().getColorName());
147        edit.setFontStyleName(fontStyle == null ? null : fontStyle.getLastDetail().getFontStyleName());
148        edit.setFontWeightName(fontWeight == null ? null : fontWeight.getLastDetail().getFontWeightName());
149        edit.setIsDefault(appearanceDetail.getIsDefault().toString());
150        edit.setSortOrder(appearanceDetail.getSortOrder().toString());
151
152        if(appearanceDescription != null) {
153            edit.setDescription(appearanceDescription.getDescription());
154        }
155    }
156
157    @Override
158    public void canUpdate(Appearance appearance) {
159        var appearanceControl = Session.getModelController(AppearanceControl.class);
160        var appearanceName = edit.getAppearanceName();
161        var duplicateAppearance = appearanceControl.getAppearanceByName(appearanceName);
162
163        if(duplicateAppearance != null && !appearance.equals(duplicateAppearance)) {
164            addExecutionError(ExecutionErrors.DuplicateAppearanceName.name(), appearanceName);
165        } else {
166            var colorControl = Session.getModelController(ColorControl.class);
167            var textColorName = edit.getTextColorName();
168            
169            textColor = textColorName == null ? null : colorControl.getColorByName(textColorName);
170            
171            if(textColorName == null || textColor != null) {
172                var backgroundColorName = edit.getBackgroundColorName();
173                
174                backgroundColor = backgroundColorName == null ? null : colorControl.getColorByName(backgroundColorName);
175
176                if(backgroundColorName == null || backgroundColor != null) {
177                    var fontStyleName = edit.getFontStyleName();
178                    
179                    fontStyle = fontStyleName == null ? null : FontLogic.getInstance().getFontStyleByName(this, fontStyleName);
180                    
181                    if(!hasExecutionErrors()) {
182                        var fontWeightName = edit.getFontWeightName();
183                        
184                        fontWeight = fontWeightName == null ? null : FontLogic.getInstance().getFontWeightByName(this, fontWeightName);
185                    }
186                } else {
187                addExecutionError(ExecutionErrors.UnknownBackgroundColorName.name(), backgroundColorName);
188                }
189            } else {
190                addExecutionError(ExecutionErrors.UnknownTextColorName.name(), textColorName);
191            }
192        }
193    }
194
195    @Override
196    public void doUpdate(Appearance appearance) {
197        var appearanceControl = Session.getModelController(AppearanceControl.class);
198        var partyPK = getPartyPK();
199        var appearanceDetailValue = appearanceControl.getAppearanceDetailValueForUpdate(appearance);
200        var appearanceDescription = appearanceControl.getAppearanceDescriptionForUpdate(appearance, getPreferredLanguage());
201        var description = edit.getDescription();
202
203        appearanceDetailValue.setAppearanceName(edit.getAppearanceName());
204        appearanceDetailValue.setTextColorPK(textColor == null ? null : textColor.getPrimaryKey());
205        appearanceDetailValue.setBackgroundColorPK(backgroundColor == null ? null : backgroundColor.getPrimaryKey());
206        appearanceDetailValue.setFontStylePK(fontStyle == null ? null : fontStyle.getPrimaryKey());
207        appearanceDetailValue.setFontWeightPK(fontWeight == null ? null : fontWeight.getPrimaryKey());
208        appearanceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
209        appearanceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
210
211        appearanceControl.updateAppearanceFromValue(appearanceDetailValue, partyPK);
212
213        if(appearanceDescription == null && description != null) {
214            appearanceControl.createAppearanceDescription(appearance, getPreferredLanguage(), description, partyPK);
215        } else {
216            if(appearanceDescription != null && description == null) {
217                appearanceControl.deleteAppearanceDescription(appearanceDescription, partyPK);
218            } else {
219                if(appearanceDescription != null && description != null) {
220                    var appearanceDescriptionValue = appearanceControl.getAppearanceDescriptionValue(appearanceDescription);
221
222                    appearanceDescriptionValue.setDescription(description);
223                    appearanceControl.updateAppearanceDescriptionFromValue(appearanceDescriptionValue, partyPK);
224                }
225            }
226        }
227    }
228    
229}