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.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.List;
032import javax.enterprise.context.Dependent;
033
034@Dependent
035public class GetContentPageAreaCommand
036        extends BaseSingleEntityCommand<ContentPageArea, GetContentPageAreaForm> {
037
038    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
047                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
048                );
049    }
050    
051    /** Creates a new instance of GetContentPageAreaCommand */
052    public GetContentPageAreaCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, false);
054    }
055    
056    @Override
057    protected ContentPageArea getEntity() {
058        var contentControl = Session.getModelController(ContentControl.class);
059        var contentCollectionName = form.getContentCollectionName();
060        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
061        ContentPageArea contentPageArea = 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                    var contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
075                    
076                    if(contentPageLayoutArea != null) {
077                        var partyControl = Session.getModelController(PartyControl.class);
078                        var languageIsoName = form.getLanguageIsoName();
079                        var language = partyControl.getLanguageByIsoName(languageIsoName);
080                        
081                        if(language != null) {
082                            contentPageArea = contentControl.getContentPageArea(contentPage, contentPageLayoutArea, language);
083                            
084                            if(contentPageArea == null) {
085                                addExecutionError(ExecutionErrors.UnknownContentPageArea.name(), contentCollectionName, contentSectionName, contentPageName,
086                                        sortOrder.toString(), languageIsoName);
087                            }
088                        } else {
089                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
090                        }
091                    } else {
092                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name(), contentCollectionName, contentSectionName, contentPageName,
093                                sortOrder.toString());
094                    }
095                } else {
096                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentCollectionName, contentSectionName, contentPageName);
097                }
098            } else {
099                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentCollectionName, contentSectionName);
100            }
101        } else {
102            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
103        }
104        
105        return contentPageArea;
106    }
107    
108    @Override
109    protected BaseResult getResult(ContentPageArea contentPageArea) {
110        var result = ContentResultFactory.getGetContentPageAreaResult();
111
112        if (contentPageArea != null) {
113            var contentControl = Session.getModelController(ContentControl.class);
114            
115            result.setContentPageArea(contentControl.getContentPageAreaTransfer(getUserVisit(), contentPageArea));
116        }
117
118        return result;
119    }
120    
121}