001/* 002 * Copyright 2018 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.plugins.odfpilotage.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.Date; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.commons.lang3.StringUtils; 026 027import org.ametys.cms.clientsideelement.SmartContentClientSideElement; 028import org.ametys.cms.repository.Content; 029import org.ametys.cms.repository.ModifiableContent; 030import org.ametys.core.ui.Callable; 031import org.ametys.core.user.UserIdentity; 032import org.ametys.odf.ODFHelper; 033import org.ametys.odf.ProgramItem; 034import org.ametys.plugins.odfpilotage.helper.PilotageStatusHelper; 035import org.ametys.plugins.odfpilotage.helper.PilotageStatusHelper.PilotageStatus; 036import org.ametys.runtime.i18n.I18nizableText; 037import org.ametys.runtime.parameter.ParameterHelper; 038import org.ametys.runtime.parameter.ParameterHelper.ParameterType; 039 040/** 041 * Client side element for pilotage status button 042 */ 043public class PilotageStatusButtonClientSideElement extends SmartContentClientSideElement 044{ 045 /** The pilotage status helper */ 046 protected PilotageStatusHelper _pilotageStatusHelper; 047 048 /** The ODF helper */ 049 protected ODFHelper _odfHelper; 050 051 @Override 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 super.service(manager); 055 _pilotageStatusHelper = (PilotageStatusHelper) manager.lookup(PilotageStatusHelper.ROLE); 056 _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE); 057 } 058 059 /** 060 * Get informations on contents' state 061 * @param contentsId the ids of contents 062 * @param buttonStatus the button status 063 * @return informations on contents' state 064 */ 065 @Callable 066 public Map<String, Object> getStatus(List<String> contentsId, String buttonStatus) 067 { 068 Map<String, Object> results = super.getStatus(contentsId); 069 070 if (!contentsId.isEmpty()) 071 { 072 String contentId = contentsId.get(0); 073 Content content = _resolver.resolveById(contentId); 074 PilotageStatus pilotageStatus = _pilotageStatusHelper.getPilotageStatus(content); 075 076 PilotageStatus buttonPilotageStatus = StringUtils.isNotBlank(buttonStatus) ? PilotageStatus.valueOf(buttonStatus.toUpperCase()) : PilotageStatus.NONE; 077 078 results.put("isEnabled", isEnable(buttonPilotageStatus, pilotageStatus)); 079 results.put("isPressed", isToggle(buttonPilotageStatus, pilotageStatus)); 080 081 results.put("go-back", _getMainlyGoBackDescription(content)); 082 results.put("go-back-disabled", _getMainlyGoBackDisabledDescription(content)); 083 results.put("go-next", _getMainlyGoNextDescription(content)); 084 results.put("go-next-disabled", _getMainlyGoBackDisabledDescription(content)); 085 } 086 087 return results; 088 } 089 090 /** 091 * Get the validation date from content id 092 * @param contentId the contentId 093 * @return the validation date 094 */ 095 @Callable 096 public Date getValidationDate(String contentId) 097 { 098 Content content = _resolver.resolveById(contentId); 099 PilotageStatus pilotageStatus = _pilotageStatusHelper.getPilotageStatus(content); 100 101 switch (pilotageStatus) 102 { 103 case MENTION_VALIDATED: 104 return _pilotageStatusHelper.getMentionValidationDate(content); 105 case ORGUNIT_VALIDATED: 106 return _pilotageStatusHelper.getOrgUnitValidationDate(content); 107 case CFVU_VALIDATED: 108 return _pilotageStatusHelper.getCFVUValidationDate(content); 109 case CFVU_MCC_VALIDATED: 110 return _pilotageStatusHelper.getCFVUMCCValidationDate(content); 111 default: 112 break; 113 } 114 115 return null; 116 } 117 118 /** 119 * Get the list of shared first child name of the content 120 * @param contentId the contentId 121 * @return the list of child name 122 */ 123 @Callable 124 public List<String> getSharedChildName(String contentId) 125 { 126 List<String> names = new ArrayList<>(); 127 128 ProgramItem programItem = _resolver.resolveById(contentId); 129 for (ProgramItem child : _odfHelper.getChildProgramItems(programItem)) 130 { 131 if (_odfHelper.getParentAbstractPrograms(child).size() > 1) 132 { 133 names.add(_contentHelper.getTitle((Content) child)); 134 } 135 } 136 137 return names; 138 } 139 140 /** 141 * True if the button is enable 142 * @param buttonPilotageStatus the button status 143 * @param contentPilotageStatus the content status 144 * @return true if the button is enable 145 */ 146 public boolean isEnable(PilotageStatus buttonPilotageStatus, PilotageStatus contentPilotageStatus) 147 { 148 switch (contentPilotageStatus) 149 { 150 case NONE: 151 return buttonPilotageStatus.equals(PilotageStatus.MENTION_VALIDATED); 152 case MENTION_VALIDATED: 153 return buttonPilotageStatus.equals(PilotageStatus.MENTION_VALIDATED) 154 || buttonPilotageStatus.equals(PilotageStatus.ORGUNIT_VALIDATED); 155 case ORGUNIT_VALIDATED: 156 return buttonPilotageStatus.equals(PilotageStatus.ORGUNIT_VALIDATED) 157 || buttonPilotageStatus.equals(PilotageStatus.CFVU_VALIDATED); 158 case CFVU_VALIDATED: 159 return buttonPilotageStatus.equals(PilotageStatus.CFVU_VALIDATED) 160 || buttonPilotageStatus.equals(PilotageStatus.CFVU_MCC_VALIDATED); 161 case CFVU_MCC_VALIDATED: 162 return buttonPilotageStatus.equals(PilotageStatus.CFVU_MCC_VALIDATED); 163 default: 164 break; 165 } 166 167 return false; 168 } 169 170 /** 171 * True if the button is toggle 172 * @param buttonPilotageStatus the button status 173 * @param contentPilotageStatus the content status 174 * @return true if the button is toggle 175 */ 176 public boolean isToggle(PilotageStatus buttonPilotageStatus, PilotageStatus contentPilotageStatus) 177 { 178 switch (contentPilotageStatus) 179 { 180 case NONE: 181 return false; 182 case MENTION_VALIDATED: 183 return buttonPilotageStatus.equals(PilotageStatus.MENTION_VALIDATED); 184 case ORGUNIT_VALIDATED: 185 return buttonPilotageStatus.equals(PilotageStatus.MENTION_VALIDATED) 186 || buttonPilotageStatus.equals(PilotageStatus.ORGUNIT_VALIDATED); 187 case CFVU_VALIDATED: 188 return buttonPilotageStatus.equals(PilotageStatus.MENTION_VALIDATED) 189 || buttonPilotageStatus.equals(PilotageStatus.ORGUNIT_VALIDATED) 190 || buttonPilotageStatus.equals(PilotageStatus.CFVU_VALIDATED); 191 case CFVU_MCC_VALIDATED: 192 return buttonPilotageStatus.equals(PilotageStatus.MENTION_VALIDATED) 193 || buttonPilotageStatus.equals(PilotageStatus.ORGUNIT_VALIDATED) 194 || buttonPilotageStatus.equals(PilotageStatus.CFVU_VALIDATED) 195 || buttonPilotageStatus.equals(PilotageStatus.CFVU_MCC_VALIDATED); 196 default: 197 break; 198 } 199 200 return false; 201 } 202 203 /** 204 * Set the pilotage status to the content 205 * @param contentId the content id 206 * @param status the pilotage status 207 * @param validationDateAsString the validation date as string 208 * @param comment the comment 209 */ 210 @Callable 211 public void setPilotageStatus(String contentId, String status, String validationDateAsString, String comment) 212 { 213 ModifiableContent content = _resolver.resolveById(contentId); 214 PilotageStatus pilotageStatus = StringUtils.isNotBlank(status) ? PilotageStatus.valueOf(status.toUpperCase()) : PilotageStatus.NONE; 215 216 UserIdentity user = _currentUserProvider.getUser(); 217 218 Date validationDate = (Date) ParameterHelper.castValue(validationDateAsString, ParameterType.DATE); 219 if (validationDate == null) 220 { 221 validationDate = getValidationDate(contentId); 222 if (validationDate == null || validationDate.before(new Date())) 223 { 224 validationDate = new Date(); 225 } 226 } 227 _pilotageStatusHelper.setValidationAttribute(content, validationDate, user, comment, pilotageStatus); 228 } 229 230 /** 231 * Remove the pilotage status from the content 232 * @param contentId the content id 233 * @param status the pilotage status 234 */ 235 @Callable 236 public void removePilotageStatus(String contentId, String status) 237 { 238 ModifiableContent content = _resolver.resolveById(contentId); 239 PilotageStatus pilotageStatus = StringUtils.isNotBlank(status) ? PilotageStatus.valueOf(status.toUpperCase()) : PilotageStatus.NONE; 240 241 _pilotageStatusHelper.removePilotageStatus(content, pilotageStatus); 242 } 243 244 /** 245 * Get content i18n description when we go back to previous step in pilotage status 246 * @param content The content 247 * @return The {@link I18nizableText} description 248 */ 249 protected I18nizableText _getMainlyGoBackDescription(Content content) 250 { 251 List<String> i18nParameters = new ArrayList<>(); 252 i18nParameters.add(_contentHelper.getTitle(content)); 253 254 I18nizableText ed = (I18nizableText) this._script.getParameters().get("go-back-content-description"); 255 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 256 } 257 258 /** 259 * Get content i18n description when we can't go back to previous step in pilotage status 260 * @param content The content 261 * @return The {@link I18nizableText} description 262 */ 263 protected I18nizableText _getMainlyGoBackDisabledDescription(Content content) 264 { 265 List<String> i18nParameters = new ArrayList<>(); 266 i18nParameters.add(_contentHelper.getTitle(content)); 267 268 I18nizableText ed = (I18nizableText) this._script.getParameters().get("go-back-disabled-content-description"); 269 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 270 } 271 272 /** 273 * Get content i18n description when we go to next step in pilotage status 274 * @param content The content 275 * @return The {@link I18nizableText} description 276 */ 277 protected I18nizableText _getMainlyGoNextDescription(Content content) 278 { 279 List<String> i18nParameters = new ArrayList<>(); 280 i18nParameters.add(_contentHelper.getTitle(content)); 281 282 I18nizableText ed = (I18nizableText) this._script.getParameters().get("go-next-content-description"); 283 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 284 } 285 286 /** 287 * Get content i18n description when we can't go to next step in pilotage status 288 * @param content The content 289 * @return The {@link I18nizableText} description 290 */ 291 protected I18nizableText _getMainlyGoNextDisabledDescription(Content content) 292 { 293 List<String> i18nParameters = new ArrayList<>(); 294 i18nParameters.add(_contentHelper.getTitle(content)); 295 296 I18nizableText ed = (I18nizableText) this._script.getParameters().get("go-next-disabled-content-description"); 297 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 298 } 299}