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