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.GetContentPageLayoutAreaForm;
020import com.echothree.control.user.content.common.result.ContentResultFactory;
021import com.echothree.control.user.content.common.result.GetContentPageLayoutAreaResult;
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.ContentPageLayout;
026import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
027import com.echothree.model.data.content.server.entity.ContentSection;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.server.control.BaseSingleEntityCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038
039public class GetContentPageLayoutAreaCommand
040        extends BaseSingleEntityCommand<ContentPageLayoutArea, GetContentPageLayoutAreaForm> {
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                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
051                ));
052    }
053    
054    /** Creates a new instance of GetContentPageLayoutAreaCommand */
055    public GetContentPageLayoutAreaCommand(UserVisitPK userVisitPK, GetContentPageLayoutAreaForm form) {
056        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
057    }
058    
059    @Override
060    protected ContentPageLayoutArea getEntity() {
061        var contentControl = Session.getModelController(ContentControl.class);
062        String contentCollectionName = form.getContentCollectionName();
063        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
064        ContentPageLayoutArea contentPageLayoutArea = null;
065        
066        if(contentCollection != null) {
067            String contentSectionName = form.getContentSectionName();
068            ContentSection contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
069            
070            if(contentSection != null) {
071                String contentPageName = form.getContentPageName();
072                ContentPage contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
073                
074                if(contentPage != null) {
075                    var sortOrder = Integer.valueOf(form.getSortOrder());
076                    ContentPageLayout contentPageLayout = contentPage.getLastDetail().getContentPageLayout();
077                    
078                    contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
079                    
080                    if(contentPageLayoutArea == null) {
081                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name());
082                    }
083                } else {
084                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
085                }
086            } else {
087                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
088            }
089        } else {
090            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
091        }
092        
093        return contentPageLayoutArea;
094    }
095    
096    @Override
097    protected BaseResult getResult(ContentPageLayoutArea contentPageLayoutArea) {
098        GetContentPageLayoutAreaResult result = ContentResultFactory.getGetContentPageLayoutAreaResult();
099
100        if (contentPageLayoutArea != null) {
101            var contentControl = Session.getModelController(ContentControl.class);
102
103            result.setContentPageLayoutArea(contentControl.getContentPageLayoutAreaTransfer(getUserVisit(), contentPageLayoutArea));
104        }
105
106        return result;
107    }
108    
109}