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