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