001/* 002 * Copyright 2010 Anyware Services 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 */ 016package org.ametys.cms.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.clientsideelement.content.SmartContentClientSideElementHelper; 027import org.ametys.cms.content.ContentHelper; 028import org.ametys.cms.repository.Content; 029import org.ametys.cms.repository.WorkflowAwareContent; 030import org.ametys.core.ui.Callable; 031import org.ametys.core.ui.StaticClientSideElement; 032import org.ametys.plugins.repository.AmetysObjectResolver; 033import org.ametys.runtime.i18n.I18nizableText; 034 035/** 036 * Edit HMI item 037 */ 038public class SmartContentClientSideElement extends StaticClientSideElement 039{ 040 /** Ametys object resolver */ 041 protected AmetysObjectResolver _resolver; 042 /** The content helper */ 043 protected ContentHelper _contentHelper; 044 /** Helper for smart content client elements */ 045 protected SmartContentClientSideElementHelper _smartHelper; 046 047 @Override 048 public void service(ServiceManager manager) throws ServiceException 049 { 050 super.service(manager); 051 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 052 _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE); 053 _smartHelper = (SmartContentClientSideElementHelper) manager.lookup(SmartContentClientSideElementHelper.ROLE); 054 } 055 056 /** 057 * Get informations on contents' state 058 * @param contentsId the ids of contents 059 * @return informations on contents' state 060 */ 061 @Callable 062 public Map<String, Object> getStatus(List<String> contentsId) 063 { 064 Map<String, Object> results = new HashMap<>(); 065 066 results.put("unmodifiable-contents", new ArrayList<Map<String, Object>>()); 067 results.put("locked-contents", new ArrayList<Map<String, Object>>()); 068 results.put("noright-contents", new ArrayList<Map<String, Object>>()); 069 results.put("invalidworkflowaction-contents", new ArrayList<Map<String, Object>>()); 070 results.put("invalidworkflowstep-contents", new ArrayList<Map<String, Object>>()); 071 results.put("allright-contents", new ArrayList<Map<String, Object>>()); 072 073 for (String contentId : contentsId) 074 { 075 Content content = _resolver.resolveById(contentId); 076 077 boolean error = false; 078 079 if (content instanceof WorkflowAwareContent) 080 { 081 // Is modifiable 082 String enabledOnModifiableOnly = (String) this._script.getParameters().get("enabled-on-modifiable-only"); 083 if ("true".equals(enabledOnModifiableOnly) && !_isModifiable(content)) 084 { 085 Map<String, Object> contentParams = getContentDefaultParameters (content); 086 contentParams.put("description", _getNoModifiableDescription(content)); 087 088 @SuppressWarnings("unchecked") 089 List<Map<String, Object>> unModifiableContents = (List<Map<String, Object>>) results.get("unmodifiable-contents"); 090 unModifiableContents.add(contentParams); 091 092 error = true; 093 } 094 095 // Is locked 096 String enabledOnUnlockOnly = (String) this._script.getParameters().get("enabled-on-unlock-only"); 097 if ("true".equals(enabledOnUnlockOnly) && _isLocked(content)) 098 { 099 Map<String, Object> contentParams = getContentDefaultParameters (content); 100 contentParams.put("description", _getLockedDescription(content)); 101 102 @SuppressWarnings("unchecked") 103 List<Map<String, Object>> lockedContents = (List<Map<String, Object>>) results.get("locked-contents"); 104 lockedContents.add(contentParams); 105 106 error = true; 107 } 108 109 // Has right correct 110 String enabledOnRightOnly = (String) this._script.getParameters().get("enabled-on-right-only"); 111 if ("true".equals(enabledOnRightOnly) && !_hasRight(content)) 112 { 113 Map<String, Object> contentParams = getContentDefaultParameters (content); 114 contentParams.put("description", _getNoRightDescription (content)); 115 116 @SuppressWarnings("unchecked") 117 List<Map<String, Object>> norightContents = (List<Map<String, Object>>) results.get("noright-contents"); 118 norightContents.add(contentParams); 119 120 error = true; 121 } 122 123 // Is workflow action correct 124 String enabledOnWorkflowActionOnly = (String) this._script.getParameters().get("enabled-on-workflow-action-only"); 125 if (enabledOnWorkflowActionOnly != null) 126 { 127 int actionId = _workflowAction(content); 128 if (actionId == -1) 129 { 130 Map<String, Object> contentParams = getContentDefaultParameters (content); 131 contentParams.put("description", _getWorkflowActionUnvailableDescription(content)); 132 133 @SuppressWarnings("unchecked") 134 List<Map<String, Object>> invalidActionContents = (List<Map<String, Object>>) results.get("invalidworkflowaction-contents"); 135 invalidActionContents.add(contentParams); 136 137 error = true; 138 } 139 else 140 { 141 results.put("workflowaction-content-actionId", actionId); 142 } 143 } 144 145 // Is workflow step correct 146 String enabledOnWorkflowStepOnly = (String) this._script.getParameters().get("enabled-on-workflow-step-only"); 147 if (enabledOnWorkflowStepOnly != null && !_isWorkflowStepCorrect(content)) 148 { 149 Map<String, Object> contentParams = getContentDefaultParameters (content); 150 contentParams.put("description", _getIncorrectWorkflowStepDescription(content)); 151 152 @SuppressWarnings("unchecked") 153 List<Map<String, Object>> invalidStepContents = (List<Map<String, Object>>) results.get("invalidworkflowstep-contents"); 154 invalidStepContents.add(contentParams); 155 156 error = true; 157 } 158 159 if (_isAllRight (content, error, results)) 160 { 161 Map<String, Object> contentParams = getContentDefaultParameters (content); 162 contentParams.put("description", _getAllRightDescription(content)); 163 164 @SuppressWarnings("unchecked") 165 List<Map<String, Object>> allrightContents = (List<Map<String, Object>>) results.get("allright-contents"); 166 allrightContents.add(contentParams); 167 } 168 } 169 } 170 171 return results; 172 } 173 174 /** 175 * Determines if the user can finally do action on content 176 * @param content The content 177 * @param hasError true if a error has already occurs 178 * @param results The result parameters to be passed to client side 179 * @return true if the user can finally do action on content 180 */ 181 protected boolean _isAllRight (Content content, boolean hasError, Map<String, Object> results) 182 { 183 return !hasError; 184 } 185 186 /** 187 * Get the default content's parameters 188 * @param content The content 189 * @return The default parameters 190 */ 191 protected Map<String, Object> getContentDefaultParameters (Content content) 192 { 193 return _smartHelper.getContentDefaultParameters(content); 194 } 195 196 /** 197 * Determines if the content is locked 198 * @param content the content 199 * @return true if the content is locked 200 */ 201 protected boolean _isLocked(Content content) 202 { 203 return _smartHelper.isLocked(content); 204 } 205 206 /** 207 * Determines if the content is modifiable 208 * @param content the content 209 * @return true if the content is modifiable 210 */ 211 protected boolean _isModifiable(Content content) 212 { 213 return _smartHelper.isModifiable(content); 214 } 215 216 /** 217 * Determines if the user has sufficient right for the given content 218 * @param content the content 219 * @return true if user has sufficient right 220 */ 221 protected boolean _hasRight(Content content) 222 { 223 return _smartHelper.hasRight(_rights, content); 224 } 225 226 /** 227 * Determines if the workflow action is correct 228 * @param content the content 229 * @return true if the workflow action is incorrect 230 */ 231 protected int _workflowAction(Content content) 232 { 233 return _smartHelper.workflowAction(_script.getParameters(), content); 234 } 235 236 /** 237 * Determines if the workflow step is correct 238 * @param content the content 239 * @return true if the workflow step is incorrect 240 */ 241 protected boolean _isWorkflowStepCorrect(Content content) 242 { 243 return _smartHelper.isWorkflowStepCorrect(_script.getParameters(), content); 244 } 245 246 /** 247 * Get i18n description when user can not do action because he has no right on content 248 * @param content The content 249 * @return The {@link I18nizableText} description 250 */ 251 protected I18nizableText _getNoRightDescription (Content content) 252 { 253 return _smartHelper.getNoRightDescription(_script.getParameters(), content); 254 } 255 256 /** 257 * Get i18n description when user can not do action because the content is locked 258 * @param content The content 259 * @return The {@link I18nizableText} description 260 */ 261 protected I18nizableText _getLockedDescription (Content content) 262 { 263 return _smartHelper.getLockedDescription(_script.getParameters(), content); 264 } 265 266 /** 267 * Get i18n description when user can not do action because there is no workflow action unvailable 268 * @param content The content 269 * @return The {@link I18nizableText} description 270 */ 271 protected I18nizableText _getWorkflowActionUnvailableDescription (Content content) 272 { 273 return _smartHelper.getWorkflowActionUnvailableDescription(_script.getParameters(), content); 274 } 275 276 /** 277 * Get i18n description when user can not do action because the workflow step is incorrect 278 * @param content The content 279 * @return The {@link I18nizableText} description 280 */ 281 protected I18nizableText _getIncorrectWorkflowStepDescription (Content content) 282 { 283 return _smartHelper.getIncorrectWorkflowStepDescription(_script.getParameters(), content); 284 } 285 286 /** 287 * Get i18n description when user can not do action because the content is not modifiable 288 * @param content The content 289 * @return The {@link I18nizableText} description 290 */ 291 protected I18nizableText _getNoModifiableDescription (Content content) 292 { 293 return _smartHelper.getNoModifiableDescription(_script.getParameters(), content); 294 } 295 296 /** 297 * Get i18n description when user can do action 298 * @param content The content 299 * @return The {@link I18nizableText} description 300 */ 301 protected I18nizableText _getAllRightDescription (Content content) 302 { 303 return _smartHelper.getAllRightDescription(_script.getParameters(), content); 304 } 305}