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.GetContentPageAreasForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.control.user.content.common.result.GetContentPageAreasResult;
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.ContentPage;
025import com.echothree.model.data.content.server.entity.ContentPageArea;
026import com.echothree.model.data.content.server.entity.ContentSection;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.Arrays;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.List;
038
039public class GetContentPageAreasCommand
040        extends BaseMultipleEntitiesCommand<ContentPageArea, GetContentPageAreasForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null)
049                ));
050    }
051    
052    /** Creates a new instance of GetContentPageAreasCommand */
053    public GetContentPageAreasCommand(UserVisitPK userVisitPK, GetContentPageAreasForm form) {
054        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
055    }
056    
057    @Override
058    protected Collection<ContentPageArea> getEntities() {
059        var contentControl = Session.getModelController(ContentControl.class);
060        String contentCollectionName = form.getContentCollectionName();
061        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
062        Collection<ContentPageArea> contentPageAreas = null;
063        
064        if(contentCollection != null) {
065            String contentSectionName = form.getContentSectionName();
066            ContentSection contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
067            
068            if(contentSection != null) {
069                String contentPageName = form.getContentPageName();
070                ContentPage contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
071                
072                if(contentPage != null) {
073                    contentPageAreas = contentControl.getContentPageAreasByContentPage(contentPage);
074                } else {
075                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
076                }
077            } else {
078                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
079            }
080        } else {
081            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
082        }
083        
084        return contentPageAreas;
085    }
086    
087    @Override
088    protected BaseResult getResult(Collection<ContentPageArea> entities) {
089        GetContentPageAreasResult result = ContentResultFactory.getGetContentPageAreasResult();
090                
091        if(entities != null) {
092            var contentControl = Session.getModelController(ContentControl.class);
093
094            result.setContentPageAreas(contentControl.getContentPageAreaTransfers(getUserVisit(), entities));
095        }
096
097        return result;
098    }
099    
100}