001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.model.control.content.server.control.ContentControl;
022import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
023import com.echothree.model.data.user.common.pk.UserVisitPK;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.common.validation.FieldType;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.server.control.BaseSingleEntityCommand;
029import com.echothree.util.server.persistence.Session;
030import java.util.Arrays;
031import java.util.Collections;
032import java.util.List;
033import javax.enterprise.context.RequestScoped;
034
035@RequestScoped
036public class GetContentPageLayoutAreaCommand
037        extends BaseSingleEntityCommand<ContentPageLayoutArea, GetContentPageLayoutAreaForm> {
038    
039    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
048                ));
049    }
050    
051    /** Creates a new instance of GetContentPageLayoutAreaCommand */
052    public GetContentPageLayoutAreaCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, false);
054    }
055    
056    @Override
057    protected ContentPageLayoutArea getEntity() {
058        var contentControl = Session.getModelController(ContentControl.class);
059        var contentCollectionName = form.getContentCollectionName();
060        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
061        ContentPageLayoutArea contentPageLayoutArea = null;
062        
063        if(contentCollection != null) {
064            var contentSectionName = form.getContentSectionName();
065            var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
066            
067            if(contentSection != null) {
068                var contentPageName = form.getContentPageName();
069                var contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
070                
071                if(contentPage != null) {
072                    var sortOrder = Integer.valueOf(form.getSortOrder());
073                    var contentPageLayout = contentPage.getLastDetail().getContentPageLayout();
074                    
075                    contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
076                    
077                    if(contentPageLayoutArea == null) {
078                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name());
079                    }
080                } else {
081                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
082                }
083            } else {
084                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
085            }
086        } else {
087            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
088        }
089        
090        return contentPageLayoutArea;
091    }
092    
093    @Override
094    protected BaseResult getResult(ContentPageLayoutArea contentPageLayoutArea) {
095        var result = ContentResultFactory.getGetContentPageLayoutAreaResult();
096
097        if (contentPageLayoutArea != null) {
098            var contentControl = Session.getModelController(ContentControl.class);
099
100            result.setContentPageLayoutArea(contentControl.getContentPageLayoutAreaTransfer(getUserVisit(), contentPageLayoutArea));
101        }
102
103        return result;
104    }
105    
106}