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.edit.ContentEditFactory;
020import com.echothree.control.user.content.common.edit.ContentPageAreaEdit;
021import com.echothree.control.user.content.common.form.EditContentPageAreaForm;
022import com.echothree.control.user.content.common.result.ContentResultFactory;
023import com.echothree.control.user.content.common.result.EditContentPageAreaResult;
024import com.echothree.control.user.content.common.spec.ContentPageAreaSpec;
025import com.echothree.model.control.content.server.control.ContentControl;
026import com.echothree.model.control.core.server.control.MimeTypeControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.data.content.server.entity.ContentPageArea;
032import com.echothree.model.data.core.server.entity.MimeType;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.command.EditMode;
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.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import com.echothree.util.server.persistence.Session;
043import java.util.List;
044import javax.enterprise.context.Dependent;
045
046@Dependent
047public class EditContentPageAreaCommand
048        extends BaseAbstractEditCommand<ContentPageAreaSpec, ContentPageAreaEdit, EditContentPageAreaResult, ContentPageArea, ContentPageArea> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.ContentPageArea.name(), SecurityRoles.Edit.name())
059                        ))
060                ));
061        
062        SPEC_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("ContentCollectionName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ContentSectionName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("ContentPageName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
067                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
068                );
069        
070        EDIT_FIELD_DEFINITIONS = List.of(
071                new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null),
072                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L),
073                new FieldDefinition("ContentPageAreaClob", FieldType.STRING, false, 1L, null),
074                new FieldDefinition("ContentPageAreaUrl", FieldType.URL, false, 1L, 200L)
075                );
076    }
077    
078    /** Creates a new instance of EditContentPageAreaCommand */
079    public EditContentPageAreaCommand() {
080        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
081    }
082    
083    @Override
084    public EditContentPageAreaResult getResult() {
085        return ContentResultFactory.getEditContentPageAreaResult();
086    }
087    
088    @Override
089    public ContentPageAreaEdit getEdit() {
090        return ContentEditFactory.getContentPageAreaEdit();
091    }
092    
093    @Override
094    public ContentPageArea getEntity(EditContentPageAreaResult result) {
095        var contentControl = Session.getModelController(ContentControl.class);
096        ContentPageArea contentPageArea = null;
097        var contentCollectionName = spec.getContentCollectionName();
098        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
099        
100        if(contentCollection != null) {
101            var contentSectionName = spec.getContentSectionName();
102            var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
103            
104            if(contentSection != null) {
105                var contentPageName = spec.getContentPageName();
106                var contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
107                
108                if(contentPage != null) {
109                    var sortOrder = Integer.valueOf(spec.getSortOrder());
110                    var contentPageLayout = contentPage.getLastDetail().getContentPageLayout();
111                    var contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
112                    
113                    if(contentPageLayoutArea != null) {
114                        var partyControl = Session.getModelController(PartyControl.class);
115                        var languageIsoName = spec.getLanguageIsoName();
116                        var language = partyControl.getLanguageByIsoName(languageIsoName);
117                        
118                        if(language != null) {
119                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
120                                contentPageArea = contentControl.getContentPageArea(contentPage, contentPageLayoutArea, language);
121                            } else { // EditMode.UPDATE
122                                contentPageArea = contentControl.getContentPageAreaForUpdate(contentPage, contentPageLayoutArea, language);
123                            }
124
125                            if(contentPageArea == null) {
126                                addExecutionError(ExecutionErrors.UnknownContentPageArea.name());
127                            }
128                        } else {
129                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
130                        }
131                    } else {
132                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name());
133                    }
134                } else {
135                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
136                }
137            } else {
138                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
139            }
140        } else {
141            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
142        }
143
144        return contentPageArea;
145    }
146    
147    @Override
148    public ContentPageArea getLockEntity(ContentPageArea contentPageArea) {
149        return contentPageArea;
150    }
151    
152    @Override
153    public void fillInResult(EditContentPageAreaResult result, ContentPageArea contentPageArea) {
154        var contentControl = Session.getModelController(ContentControl.class);
155        
156        result.setContentPageArea(contentControl.getContentPageAreaTransfer(getUserVisit(), contentPageArea));
157    }
158    
159    @Override
160    public void doLock(ContentPageAreaEdit edit, ContentPageArea contentPageArea) {
161        var contentControl = Session.getModelController(ContentControl.class);
162        var contentPageAreaDetail = contentPageArea.getLastDetail();
163        var contentPageAreaBlob = contentControl.getContentPageAreaBlob(contentPageAreaDetail);
164        var contentPageAreaClob = contentControl.getContentPageAreaClob(contentPageAreaDetail);
165        var contentPageAreaString = contentControl.getContentPageAreaString(contentPageAreaDetail);
166        var contentPageAreaUrl = contentControl.getContentPageAreaUrl(contentPageAreaDetail);
167
168        edit.setMimeTypeName(contentPageArea.getLastDetail().getMimeType().getLastDetail().getMimeTypeName());
169
170        if(contentPageAreaBlob != null) {
171            edit.setContentPageAreaBlob(contentPageAreaBlob.getBlob());
172        }
173
174        if(contentPageAreaClob != null) {
175            edit.setContentPageAreaClob(contentPageAreaClob.getClob());
176        }
177
178        if(contentPageAreaString != null) {
179            edit.setDescription(contentPageAreaString.getString());
180        }
181
182        if(contentPageAreaUrl != null) {
183            edit.setContentPageAreaUrl(contentPageAreaUrl.getUrl());
184        }
185    }
186    
187   MimeType mimeType = null;
188    
189    @Override
190    public void canUpdate(ContentPageArea contentPageArea) {
191        var mimeTypeControl = Session.getModelController(MimeTypeControl.class);
192        var mimeTypeName = edit.getMimeTypeName();
193        
194        mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName);
195
196        if(mimeType == null) {
197            addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
198        }
199    }
200    
201    @Override
202    public void doUpdate(ContentPageArea contentPageArea) {
203        var contentControl = Session.getModelController(ContentControl.class);
204        var contentPageAreaDetailValue = contentControl.getContentPageAreaDetailValueForUpdate(contentPageArea);
205        var description = edit.getDescription();
206        var contentPageAreaBlob = edit.getContentPageAreaBlob();
207        var contentPageAreaClob = edit.getContentPageAreaClob();
208        var contentPageAreaUrl = edit.getContentPageAreaUrl();
209
210        contentPageAreaDetailValue.setMimeTypePK(mimeType.getPrimaryKey());
211
212        var contentPageAreaDetail = contentControl.updateContentPageAreaFromValue(contentPageAreaDetailValue, true, getPartyPK());
213
214        if(contentPageAreaBlob != null) {
215            contentControl.createContentPageAreaBlob(contentPageAreaDetail, contentPageAreaBlob);
216        }
217
218        if(contentPageAreaClob != null) {
219            contentControl.createContentPageAreaClob(contentPageAreaDetail, contentPageAreaClob);
220        }
221
222        if(contentPageAreaUrl != null) {
223            contentControl.createContentPageAreaUrl(contentPageAreaDetail, contentPageAreaUrl);
224        }
225
226        if(description != null) {
227            contentControl.createContentPageAreaString(contentPageAreaDetail, description);
228        }
229    }
230    
231}