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.CreateContentPageAreaForm;
020import com.echothree.model.control.content.server.control.ContentControl;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.data.content.server.entity.ContentCollection;
026import com.echothree.model.data.content.server.entity.ContentPage;
027import com.echothree.model.data.content.server.entity.ContentPageArea;
028import com.echothree.model.data.content.server.entity.ContentPageAreaDetail;
029import com.echothree.model.data.content.server.entity.ContentPageLayout;
030import com.echothree.model.data.content.server.entity.ContentPageLayoutArea;
031import com.echothree.model.data.content.server.entity.ContentSection;
032import com.echothree.model.data.core.server.entity.MimeType;
033import com.echothree.model.data.party.server.entity.Language;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.common.command.BaseResult;
039import com.echothree.util.common.persistence.type.ByteArray;
040import com.echothree.util.server.control.BaseSimpleCommand;
041import com.echothree.util.server.control.CommandSecurityDefinition;
042import com.echothree.util.server.control.PartyTypeDefinition;
043import com.echothree.util.server.control.SecurityRoleDefinition;
044import com.echothree.util.server.persistence.Session;
045import java.util.Arrays;
046import java.util.Collections;
047import java.util.List;
048
049public class CreateContentPageAreaCommand
050        extends BaseSimpleCommand<CreateContentPageAreaForm> {
051    
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
054    
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
059                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPageArea.name(), SecurityRoles.Create.name())
060                        )))
061                )));
062        
063        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
064                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
068                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L),
071                // ContentPageAreaBlob is not validated
072                new FieldDefinition("ContentPageAreaClob", FieldType.STRING, false, 1L, null),
073                new FieldDefinition("ContentPageAreaUrl", FieldType.URL, false, 1L, 200L)
074                ));
075    }
076    
077    /** Creates a new instance of CreateContentPageAreaCommand */
078    public CreateContentPageAreaCommand(UserVisitPK userVisitPK, CreateContentPageAreaForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
080    }
081    
082    @Override
083    protected BaseResult execute() {
084        var contentControl = Session.getModelController(ContentControl.class);
085        String contentCollectionName = form.getContentCollectionName();
086        ContentCollection contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
087        
088        if(contentCollection != null) {
089            String contentSectionName = form.getContentSectionName();
090            ContentSection contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
091            
092            if(contentSection != null) {
093                String contentPageName = form.getContentPageName();
094                ContentPage contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
095                
096                if(contentPage != null) {
097                    var sortOrder = Integer.valueOf(form.getSortOrder());
098                    ContentPageLayout contentPageLayout = contentPage.getLastDetail().getContentPageLayout();
099                    ContentPageLayoutArea contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
100                    
101                    if(contentPageLayoutArea != null) {
102                        var partyControl = Session.getModelController(PartyControl.class);
103                        String languageIsoName = form.getLanguageIsoName();
104                        Language language = partyControl.getLanguageByIsoName(languageIsoName);
105                        
106                        if(language != null) {
107                            ContentPageArea contentPageArea = contentControl.getContentPageArea(contentPage, contentPageLayoutArea, language);
108                            
109                            if(contentPageArea == null) {
110                                var coreControl = getCoreControl();
111                                String mimeTypeName = form.getMimeTypeName();
112                                MimeType mimeType = coreControl.getMimeTypeByName(mimeTypeName);
113                                
114                                if(mimeType != null) {
115                                    var description = form.getDescription();
116                                    ByteArray contentPageAreaBlob = form.getContentPageAreaBlob();
117                                    String contentPageAreaClob = form.getContentPageAreaClob();
118                                    String contentPageAreaUrl = form.getContentPageAreaUrl();
119                                    
120                                    contentPageArea = contentControl.createContentPageArea(contentPage, contentPageLayoutArea, language, mimeType, getPartyPK());
121                                    ContentPageAreaDetail contentPageAreaDetail = contentPageArea.getLastDetail();
122                                    
123                                    if(contentPageAreaBlob != null)
124                                        contentControl.createContentPageAreaBlob(contentPageAreaDetail, contentPageAreaBlob);
125                                    
126                                    if(contentPageAreaClob != null)
127                                        contentControl.createContentPageAreaClob(contentPageAreaDetail, contentPageAreaClob);
128                                    
129                                    if(contentPageAreaUrl != null)
130                                        contentControl.createContentPageAreaUrl(contentPageAreaDetail, contentPageAreaUrl);
131                                    
132                                    if(description != null)
133                                        contentControl.createContentPageAreaString(contentPageAreaDetail, description);
134                                } else {
135                                    addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
136                                }
137                            } else {
138                                addExecutionError(ExecutionErrors.DuplicateContentPageArea.name());
139                            }
140                        } else {
141                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
142                        }
143                    } else {
144                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name());
145                    }
146                } else {
147                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
148                }
149            } else {
150                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
151            }
152        } else {
153            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
154        }
155        
156        return null;
157    }
158    
159}