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 java.util.List;
036import javax.enterprise.context.Dependent;
037import javax.inject.Inject;
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    @Inject
058    ForumControl forumControl;
059
060    @Inject
061    PartyControl partyControl;
062
063    
064    /** Creates a new instance of EditForumGroupDescriptionCommand */
065    public EditForumGroupDescriptionCommand() {
066        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
067    }
068
069    @Override
070    public EditForumGroupDescriptionResult getResult() {
071        return ForumResultFactory.getEditForumGroupDescriptionResult();
072    }
073
074    @Override
075    public ForumGroupDescriptionEdit getEdit() {
076        return ForumEditFactory.getForumGroupDescriptionEdit();
077    }
078
079    @Override
080    public ForumGroupDescription getEntity(EditForumGroupDescriptionResult result) {
081        ForumGroupDescription forumGroupDescription = null;
082        var forumGroupName = spec.getForumGroupName();
083        var forumGroup = forumControl.getForumGroupByName(forumGroupName);
084
085        if(forumGroup != null) {
086            var languageIsoName = spec.getLanguageIsoName();
087            var language = partyControl.getLanguageByIsoName(languageIsoName);
088
089            if(language != null) {
090                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
091                    forumGroupDescription = forumControl.getForumGroupDescription(forumGroup, language);
092                } else { // EditMode.UPDATE
093                    forumGroupDescription = forumControl.getForumGroupDescriptionForUpdate(forumGroup, language);
094                }
095
096                if(forumGroupDescription == null) {
097                    addExecutionError(ExecutionErrors.UnknownForumGroupDescription.name(), forumGroupName, languageIsoName);
098                }
099            } else {
100                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
101            }
102        } else {
103            addExecutionError(ExecutionErrors.UnknownForumGroupName.name(), forumGroupName);
104        }
105
106        return forumGroupDescription;
107    }
108
109    @Override
110    public ForumGroup getLockEntity(ForumGroupDescription forumGroupDescription) {
111        return forumGroupDescription.getForumGroup();
112    }
113
114    @Override
115    public void fillInResult(EditForumGroupDescriptionResult result, ForumGroupDescription forumGroupDescription) {
116        result.setForumGroupDescription(forumControl.getForumGroupDescriptionTransfer(getUserVisit(), forumGroupDescription));
117    }
118
119    @Override
120    public void doLock(ForumGroupDescriptionEdit edit, ForumGroupDescription forumGroupDescription) {
121        edit.setDescription(forumGroupDescription.getDescription());
122    }
123
124    @Override
125    public void doUpdate(ForumGroupDescription forumGroupDescription) {
126        var forumGroupDescriptionValue = forumControl.getForumGroupDescriptionValue(forumGroupDescription);
127
128        forumGroupDescriptionValue.setDescription(edit.getDescription());
129
130        forumControl.updateForumGroupDescriptionFromValue(forumGroupDescriptionValue, getPartyPK());
131    }
132    
133}