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