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