001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.control.user.content.common.result.GetContentPageLayoutAreasResult;
022import com.echothree.model.control.content.server.control.ContentControl;
023import com.echothree.model.data.content.server.entity.ContentCollection;
024import com.echothree.model.data.content.server.entity.ContentPage;
025import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
026import com.echothree.model.data.content.server.entity.ContentSection;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.BaseResult;
032import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.Arrays;
035import java.util.Collection;
036import java.util.Collections;
037import java.util.List;
038
039public class GetContentPageLayoutAreasCommand
040        extends BaseMultipleEntitiesCommand<ContentPageLayoutArea, GetContentPageLayoutAreasForm> {
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 GetContentPageLayoutAreasCommand */
054    public GetContentPageLayoutAreasCommand(UserVisitPK userVisitPK, GetContentPageLayoutAreasForm form) {
055        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
056    }
057    
058    @Override
059    protected Collection<ContentPageLayoutArea> getEntities() {
060        var contentControl = Session.getModelController(ContentControl.class);
061        String contentCollectionName = form.getContentCollectionName();
062        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
063        Collection<ContentPageLayoutArea> contentPageLayoutAreas = null;
064        
065        if(contentCollection != null) {
066            String contentSectionName = form.getContentSectionName();
067            ContentSection contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
068            
069            if(contentSection != null) {
070                String contentPageName = form.getContentPageName();
071                ContentPage contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
072                
073                if(contentPage != null) {
074                    contentPageLayoutAreas = contentControl.getContentPageLayoutAreasByContentPageLayout(contentPage.getLastDetail().getContentPageLayout());
075                } else {
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        return contentPageLayoutAreas;
086    }
087    
088    @Override
089    protected BaseResult getResult(Collection<ContentPageLayoutArea> entities) {
090        GetContentPageLayoutAreasResult result = ContentResultFactory.getGetContentPageLayoutAreasResult();
091        
092        if(entities != null) {
093            var contentControl = Session.getModelController(ContentControl.class);
094            
095            result.setContentPageLayoutAreas(contentControl.getContentPageLayoutAreaTransfers(getUserVisit(), entities));
096        }
097        
098        return result;
099    }
100    
101}