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