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.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.Collection;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036
037@Dependent
038public class GetContentPageLayoutAreasCommand
039        extends BasePaginatedMultipleEntitiesCommand<ContentPageLayoutArea, GetContentPageLayoutAreasForm> {
040    
041    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
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 GetContentPageLayoutAreasCommand */
053    public GetContentPageLayoutAreasCommand() {
054        super(null, FORM_FIELD_DEFINITIONS, false);
055    }
056
057    ContentPage contentPage;
058    
059    @Override
060    protected void handleForm() {
061        var contentControl = Session.getModelController(ContentControl.class);
062        var contentCollectionName = form.getContentCollectionName();
063        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
064
065        if(contentCollection != null) {
066            var contentSectionName = form.getContentSectionName();
067            var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
068
069            if(contentSection != null) {
070                var contentPageName = form.getContentPageName();
071
072                contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
073
074                if(contentPage == null) {
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    
085    @Override
086    protected Long getTotalEntities() {
087        var contentControl = Session.getModelController(ContentControl.class);
088
089        return hasExecutionErrors() ? null :
090                contentControl.countContentPageLayoutAreasByContentPageLayout(contentPage.getLastDetail().getContentPageLayout());
091    }
092    
093    @Override
094    protected Collection<ContentPageLayoutArea> getEntities() {
095        Collection<ContentPageLayoutArea> contentPageLayoutAreas = null;
096
097        if(!hasExecutionErrors()) {
098            var contentControl = Session.getModelController(ContentControl.class);
099
100            contentPageLayoutAreas = contentControl.getContentPageLayoutAreasByContentPageLayout(contentPage.getLastDetail().getContentPageLayout());
101        }
102
103        return contentPageLayoutAreas;
104    }
105    
106    @Override
107    protected BaseResult getResult(Collection<ContentPageLayoutArea> entities) {
108        var result = ContentResultFactory.getGetContentPageLayoutAreasResult();
109        
110        if(entities != null) {
111            var contentControl = Session.getModelController(ContentControl.class);
112            var userVisit = getUserVisit();
113
114            result.setContentPage(contentControl.getContentPageTransfer(userVisit, contentPage));
115
116            if(session.hasLimit(ContentPageLayoutAreaFactory.class)) {
117                result.setContentPageLayoutAreaCount(getTotalEntities());
118            }
119
120            result.setContentPageLayoutAreas(contentControl.getContentPageLayoutAreaTransfers(userVisit, entities));
121        }
122        
123        return result;
124    }
125    
126}