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 java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
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    @Inject
052    ContentControl contentControl;
053
054    @Inject
055    PartyControl partyControl;
056
057    
058    /** Creates a new instance of GetContentPageAreaCommand */
059    public GetContentPageAreaCommand() {
060        super(null, FORM_FIELD_DEFINITIONS, false);
061    }
062    
063    @Override
064    protected ContentPageArea getEntity() {
065        var contentCollectionName = form.getContentCollectionName();
066        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
067        ContentPageArea contentPageArea = null;
068
069        if(contentCollection != null) {
070            var contentSectionName = form.getContentSectionName();
071            var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
072            
073            if(contentSection != null) {
074                var contentPageName = form.getContentPageName();
075                var contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
076                
077                if(contentPage != null) {
078                    var sortOrder = Integer.valueOf(form.getSortOrder());
079                    var contentPageLayout = contentPage.getLastDetail().getContentPageLayout();
080                    var contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
081                    
082                    if(contentPageLayoutArea != null) {
083                        var languageIsoName = form.getLanguageIsoName();
084                        var language = partyControl.getLanguageByIsoName(languageIsoName);
085                        
086                        if(language != null) {
087                            contentPageArea = contentControl.getContentPageArea(contentPage, contentPageLayoutArea, language);
088                            
089                            if(contentPageArea == null) {
090                                addExecutionError(ExecutionErrors.UnknownContentPageArea.name(), contentCollectionName, contentSectionName, contentPageName,
091                                        sortOrder.toString(), languageIsoName);
092                            }
093                        } else {
094                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
095                        }
096                    } else {
097                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name(), contentCollectionName, contentSectionName, contentPageName,
098                                sortOrder.toString());
099                    }
100                } else {
101                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentCollectionName, contentSectionName, contentPageName);
102                }
103            } else {
104                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentCollectionName, contentSectionName);
105            }
106        } else {
107            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
108        }
109        
110        return contentPageArea;
111    }
112    
113    @Override
114    protected BaseResult getResult(ContentPageArea contentPageArea) {
115        var result = ContentResultFactory.getGetContentPageAreaResult();
116
117        if (contentPageArea != null) {
118            result.setContentPageArea(contentControl.getContentPageAreaTransfer(getUserVisit(), contentPageArea));
119        }
120
121        return result;
122    }
123    
124}