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 java.util.Collection;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
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    @Inject
053    ContentControl contentControl;
054
055    
056    /** Creates a new instance of GetContentPageLayoutAreasCommand */
057    public GetContentPageLayoutAreasCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, true);
059    }
060
061    ContentPage contentPage;
062    
063    @Override
064    protected void handleForm() {
065        var contentCollectionName = form.getContentCollectionName();
066        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
067
068        if(contentCollection != null) {
069            var contentSectionName = form.getContentSectionName();
070            var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
071
072            if(contentSection != null) {
073                var contentPageName = form.getContentPageName();
074
075                contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
076
077                if(contentPage == null) {
078                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
079                }
080            } else {
081                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
082            }
083        } else {
084            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
085        }
086    }
087    
088    @Override
089    protected Long getTotalEntities() {
090        return hasExecutionErrors() ? null :
091                contentControl.countContentPageLayoutAreasByContentPageLayout(contentPage.getLastDetail().getContentPageLayout());
092    }
093    
094    @Override
095    protected Collection<ContentPageLayoutArea> getEntities() {
096        Collection<ContentPageLayoutArea> contentPageLayoutAreas = null;
097
098        if(!hasExecutionErrors()) {
099            contentPageLayoutAreas = contentControl.getContentPageLayoutAreasByContentPageLayout(contentPage.getLastDetail().getContentPageLayout());
100        }
101
102        return contentPageLayoutAreas;
103    }
104    
105    @Override
106    protected BaseResult getResult(Collection<ContentPageLayoutArea> entities) {
107        var result = ContentResultFactory.getGetContentPageLayoutAreasResult();
108        
109        if(entities != null) {
110            var userVisit = getUserVisit();
111
112            result.setContentPage(contentControl.getContentPageTransfer(userVisit, contentPage));
113
114            if(session.hasLimit(ContentPageLayoutAreaFactory.class)) {
115                result.setContentPageLayoutAreaCount(getTotalEntities());
116            }
117
118            result.setContentPageLayoutAreas(contentControl.getContentPageLayoutAreaTransfers(userVisit, entities));
119        }
120        
121        return result;
122    }
123    
124}