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.forum.server.command;
018
019import com.echothree.control.user.forum.common.edit.ForumEditFactory;
020import com.echothree.control.user.forum.common.edit.ForumGroupDescriptionEdit;
021import com.echothree.control.user.forum.common.form.EditForumGroupDescriptionForm;
022import com.echothree.control.user.forum.common.result.EditForumGroupDescriptionResult;
023import com.echothree.control.user.forum.common.result.ForumResultFactory;
024import com.echothree.control.user.forum.common.spec.ForumGroupDescriptionSpec;
025import com.echothree.model.control.forum.server.control.ForumControl;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.data.forum.server.entity.ForumGroup;
028import com.echothree.model.data.forum.server.entity.ForumGroupDescription;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.EditMode;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038
039@Dependent
040public class EditForumGroupDescriptionCommand
041        extends BaseAbstractEditCommand<ForumGroupDescriptionSpec, ForumGroupDescriptionEdit, EditForumGroupDescriptionResult, ForumGroupDescription, ForumGroup> {
042    
043    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
044    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
045    
046    static {
047        SPEC_FIELD_DEFINITIONS = List.of(
048                new FieldDefinition("ForumGroupName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
050                );
051
052        EDIT_FIELD_DEFINITIONS = List.of(
053                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
054                );
055    }
056    
057    /** Creates a new instance of EditForumGroupDescriptionCommand */
058    public EditForumGroupDescriptionCommand() {
059        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
060    }
061
062    @Override
063    public EditForumGroupDescriptionResult getResult() {
064        return ForumResultFactory.getEditForumGroupDescriptionResult();
065    }
066
067    @Override
068    public ForumGroupDescriptionEdit getEdit() {
069        return ForumEditFactory.getForumGroupDescriptionEdit();
070    }
071
072    @Override
073    public ForumGroupDescription getEntity(EditForumGroupDescriptionResult result) {
074        var forumControl = Session.getModelController(ForumControl.class);
075        ForumGroupDescription forumGroupDescription = null;
076        var forumGroupName = spec.getForumGroupName();
077        var forumGroup = forumControl.getForumGroupByName(forumGroupName);
078
079        if(forumGroup != null) {
080            var partyControl = Session.getModelController(PartyControl.class);
081            var languageIsoName = spec.getLanguageIsoName();
082            var language = partyControl.getLanguageByIsoName(languageIsoName);
083
084            if(language != null) {
085                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
086                    forumGroupDescription = forumControl.getForumGroupDescription(forumGroup, language);
087                } else { // EditMode.UPDATE
088                    forumGroupDescription = forumControl.getForumGroupDescriptionForUpdate(forumGroup, language);
089                }
090
091                if(forumGroupDescription == null) {
092                    addExecutionError(ExecutionErrors.UnknownForumGroupDescription.name(), forumGroupName, languageIsoName);
093                }
094            } else {
095                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
096            }
097        } else {
098            addExecutionError(ExecutionErrors.UnknownForumGroupName.name(), forumGroupName);
099        }
100
101        return forumGroupDescription;
102    }
103
104    @Override
105    public ForumGroup getLockEntity(ForumGroupDescription forumGroupDescription) {
106        return forumGroupDescription.getForumGroup();
107    }
108
109    @Override
110    public void fillInResult(EditForumGroupDescriptionResult result, ForumGroupDescription forumGroupDescription) {
111        var forumControl = Session.getModelController(ForumControl.class);
112
113        result.setForumGroupDescription(forumControl.getForumGroupDescriptionTransfer(getUserVisit(), forumGroupDescription));
114    }
115
116    @Override
117    public void doLock(ForumGroupDescriptionEdit edit, ForumGroupDescription forumGroupDescription) {
118        edit.setDescription(forumGroupDescription.getDescription());
119    }
120
121    @Override
122    public void doUpdate(ForumGroupDescription forumGroupDescription) {
123        var forumControl = Session.getModelController(ForumControl.class);
124        var forumGroupDescriptionValue = forumControl.getForumGroupDescriptionValue(forumGroupDescription);
125
126        forumGroupDescriptionValue.setDescription(edit.getDescription());
127
128        forumControl.updateForumGroupDescriptionFromValue(forumGroupDescriptionValue, getPartyPK());
129    }
130    
131}