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.content.server.command;
018
019import com.echothree.control.user.content.common.form.GetContentForumsForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.control.user.content.common.result.GetContentForumsResult;
022import com.echothree.model.control.content.server.control.ContentControl;
023import com.echothree.model.data.content.server.entity.ContentCollection;
024import com.echothree.model.data.content.server.entity.ContentForum;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collection;
035import java.util.Collections;
036import java.util.List;
037
038public class GetContentForumsCommand
039        extends BaseMultipleEntitiesCommand<ContentForum, GetContentForumsForm> {
040    
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
045                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null)
046                ));
047    }
048    
049    /** Creates a new instance of GetContentForumsCommand */
050    public GetContentForumsCommand(UserVisitPK userVisitPK, GetContentForumsForm form) {
051        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
052    }
053    
054    private ContentCollection contentCollection;
055    
056    @Override
057    protected Collection<ContentForum> getEntities() {
058        var contentControl = Session.getModelController(ContentControl.class);
059        String contentCollectionName = form.getContentCollectionName();
060        Collection<ContentForum> contentForums = null;
061        
062        contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
063        
064        if(contentCollection != null) {
065            contentForums = contentControl.getContentForums(contentCollection);
066        } else {
067            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
068        }
069        
070        return contentForums;
071    }
072    
073    @Override
074    protected BaseResult getResult(Collection<ContentForum> entities) {
075        GetContentForumsResult result = ContentResultFactory.getGetContentForumsResult();
076        
077        if(entities != null) {
078            var contentControl = Session.getModelController(ContentControl.class);
079            UserVisit userVisit = getUserVisit();
080            
081            result.setContentCollection(contentControl.getContentCollectionTransfer(userVisit, contentCollection));
082            result.setContentForums(contentControl.getContentForumTransfers(userVisit, entities));
083        }
084        
085        return result;
086    }
087    
088}