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 java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
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    @Inject
079    ContentControl contentControl;
080
081    @Inject
082    MimeTypeControl mimeTypeControl;
083
084    @Inject
085    PartyControl partyControl;
086
087    
088    /** Creates a new instance of EditContentPageAreaCommand */
089    public EditContentPageAreaCommand() {
090        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
091    }
092    
093    @Override
094    public EditContentPageAreaResult getResult() {
095        return ContentResultFactory.getEditContentPageAreaResult();
096    }
097    
098    @Override
099    public ContentPageAreaEdit getEdit() {
100        return ContentEditFactory.getContentPageAreaEdit();
101    }
102    
103    @Override
104    public ContentPageArea getEntity(EditContentPageAreaResult result) {
105        ContentPageArea contentPageArea = null;
106        var contentCollectionName = spec.getContentCollectionName();
107        var contentCollection = contentControl.getContentCollectionByName(contentCollectionName);
108        
109        if(contentCollection != null) {
110            var contentSectionName = spec.getContentSectionName();
111            var contentSection = contentControl.getContentSectionByName(contentCollection, contentSectionName);
112            
113            if(contentSection != null) {
114                var contentPageName = spec.getContentPageName();
115                var contentPage = contentControl.getContentPageByName(contentSection, contentPageName);
116                
117                if(contentPage != null) {
118                    var sortOrder = Integer.valueOf(spec.getSortOrder());
119                    var contentPageLayout = contentPage.getLastDetail().getContentPageLayout();
120                    var contentPageLayoutArea = contentControl.getContentPageLayoutArea(contentPageLayout, sortOrder);
121                    
122                    if(contentPageLayoutArea != null) {
123                        var languageIsoName = spec.getLanguageIsoName();
124                        var language = partyControl.getLanguageByIsoName(languageIsoName);
125                        
126                        if(language != null) {
127                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
128                                contentPageArea = contentControl.getContentPageArea(contentPage, contentPageLayoutArea, language);
129                            } else { // EditMode.UPDATE
130                                contentPageArea = contentControl.getContentPageAreaForUpdate(contentPage, contentPageLayoutArea, language);
131                            }
132
133                            if(contentPageArea == null) {
134                                addExecutionError(ExecutionErrors.UnknownContentPageArea.name());
135                            }
136                        } else {
137                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
138                        }
139                    } else {
140                        addExecutionError(ExecutionErrors.UnknownContentPageLayoutArea.name());
141                    }
142                } else {
143                    addExecutionError(ExecutionErrors.UnknownContentPageName.name(), contentPageName);
144                }
145            } else {
146                addExecutionError(ExecutionErrors.UnknownContentSectionName.name(), contentSectionName);
147            }
148        } else {
149            addExecutionError(ExecutionErrors.UnknownContentCollectionName.name(), contentCollectionName);
150        }
151
152        return contentPageArea;
153    }
154    
155    @Override
156    public ContentPageArea getLockEntity(ContentPageArea contentPageArea) {
157        return contentPageArea;
158    }
159    
160    @Override
161    public void fillInResult(EditContentPageAreaResult result, ContentPageArea contentPageArea) {
162        result.setContentPageArea(contentControl.getContentPageAreaTransfer(getUserVisit(), contentPageArea));
163    }
164    
165    @Override
166    public void doLock(ContentPageAreaEdit edit, ContentPageArea contentPageArea) {
167        var contentPageAreaDetail = contentPageArea.getLastDetail();
168        var contentPageAreaBlob = contentControl.getContentPageAreaBlob(contentPageAreaDetail);
169        var contentPageAreaClob = contentControl.getContentPageAreaClob(contentPageAreaDetail);
170        var contentPageAreaString = contentControl.getContentPageAreaString(contentPageAreaDetail);
171        var contentPageAreaUrl = contentControl.getContentPageAreaUrl(contentPageAreaDetail);
172
173        edit.setMimeTypeName(contentPageArea.getLastDetail().getMimeType().getLastDetail().getMimeTypeName());
174
175        if(contentPageAreaBlob != null) {
176            edit.setContentPageAreaBlob(contentPageAreaBlob.getBlob());
177        }
178
179        if(contentPageAreaClob != null) {
180            edit.setContentPageAreaClob(contentPageAreaClob.getClob());
181        }
182
183        if(contentPageAreaString != null) {
184            edit.setDescription(contentPageAreaString.getString());
185        }
186
187        if(contentPageAreaUrl != null) {
188            edit.setContentPageAreaUrl(contentPageAreaUrl.getUrl());
189        }
190    }
191    
192   MimeType mimeType = null;
193    
194    @Override
195    public void canUpdate(ContentPageArea contentPageArea) {
196        var mimeTypeName = edit.getMimeTypeName();
197        
198        mimeType = mimeTypeControl.getMimeTypeByName(mimeTypeName);
199
200        if(mimeType == null) {
201            addExecutionError(ExecutionErrors.UnknownMimeTypeName.name(), mimeTypeName);
202        }
203    }
204    
205    @Override
206    public void doUpdate(ContentPageArea contentPageArea) {
207        var contentPageAreaDetailValue = contentControl.getContentPageAreaDetailValueForUpdate(contentPageArea);
208        var description = edit.getDescription();
209        var contentPageAreaBlob = edit.getContentPageAreaBlob();
210        var contentPageAreaClob = edit.getContentPageAreaClob();
211        var contentPageAreaUrl = edit.getContentPageAreaUrl();
212
213        contentPageAreaDetailValue.setMimeTypePK(mimeType.getPrimaryKey());
214
215        var contentPageAreaDetail = contentControl.updateContentPageAreaFromValue(contentPageAreaDetailValue, true, getPartyPK());
216
217        if(contentPageAreaBlob != null) {
218            contentControl.createContentPageAreaBlob(contentPageAreaDetail, contentPageAreaBlob);
219        }
220
221        if(contentPageAreaClob != null) {
222            contentControl.createContentPageAreaClob(contentPageAreaDetail, contentPageAreaClob);
223        }
224
225        if(contentPageAreaUrl != null) {
226            contentControl.createContentPageAreaUrl(contentPageAreaDetail, contentPageAreaUrl);
227        }
228
229        if(description != null) {
230            contentControl.createContentPageAreaString(contentPageAreaDetail, description);
231        }
232    }
233    
234}