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.content.server.command;
018
019import com.echothree.control.user.content.common.form.GetContentPageLayoutAreasForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.model.control.content.server.control.ContentControl;
022import com.echothree.model.data.content.server.entity.ContentPage;
023import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
024import com.echothree.model.data.content.server.factory.ContentPageFactory;
025import com.echothree.model.data.content.server.factory.ContentPageLayoutAreaFactory;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
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.server.control.BasePaginatedMultipleEntitiesCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collection;
035import java.util.Collections;
036import java.util.List;
037import javax.enterprise.context.RequestScoped;
038
039@RequestScoped
040public class GetContentPageLayoutAreasCommand
041        extends BasePaginatedMultipleEntitiesCommand<ContentPageLayoutArea, GetContentPageLayoutAreasForm> {
042    
043    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null)
051                ));
052    }
053    
054    /** Creates a new instance of GetContentPageLayoutAreasCommand */
055    public GetContentPageLayoutAreasCommand() {
056        super(null, FORM_FIELD_DEFINITIONS, false);
057    }
058
059    ContentPage contentPage;
060    
061    @Override
062    protected void handleForm() {
063        var contentControl = Session.getModelController(ContentControl.class);
064        var contentCollectionName = form.getContentCollectionName();
065        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
066
067        if(contentCollection != null) {
068            var contentSectionName = form.getContentSectionName();
069            var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
070
071            if(contentSection != null) {
072                var contentPageName = form.getContentPageName();
073
074                contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
075
076                if(contentPage == null) {
077                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
078                }
079            } else {
080                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
081            }
082        } else {
083            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
084        }
085    }
086    
087    @Override
088    protected Long getTotalEntities() {
089        var contentControl = Session.getModelController(ContentControl.class);
090
091        return hasExecutionErrors() ? null :
092                contentControl.countContentPageLayoutAreasByContentPageLayout(contentPage.getLastDetail().getContentPageLayout());
093    }
094    
095    @Override
096    protected Collection<ContentPageLayoutArea> getEntities() {
097        Collection<ContentPageLayoutArea> contentPageLayoutAreas = null;
098
099        if(!hasExecutionErrors()) {
100            var contentControl = Session.getModelController(ContentControl.class);
101
102            contentPageLayoutAreas = contentControl.getContentPageLayoutAreasByContentPageLayout(contentPage.getLastDetail().getContentPageLayout());
103        }
104
105        return contentPageLayoutAreas;
106    }
107    
108    @Override
109    protected BaseResult getResult(Collection<ContentPageLayoutArea> entities) {
110        var result = ContentResultFactory.getGetContentPageLayoutAreasResult();
111        
112        if(entities != null) {
113            var contentControl = Session.getModelController(ContentControl.class);
114            var userVisit = getUserVisit();
115
116            result.setContentPage(contentControl.getContentPageTransfer(userVisit, contentPage));
117
118            if(session.hasLimit(ContentPageLayoutAreaFactory.class)) {
119                result.setContentPageLayoutAreaCount(getTotalEntities());
120            }
121
122            result.setContentPageLayoutAreas(contentControl.getContentPageLayoutAreaTransfers(userVisit, entities));
123        }
124        
125        return result;
126    }
127    
128}