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