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.odf.orgunit; 017 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.Collections; 021import java.util.List; 022import java.util.Optional; 023 024import javax.jcr.Node; 025 026import org.apache.commons.lang3.ArrayUtils; 027import org.apache.commons.lang3.StringUtils; 028 029import org.ametys.cms.content.external.ExternalizableMetadataHelper; 030import org.ametys.cms.content.external.ExternalizableMetadataProvider.ExternalizableMetadataStatus; 031import org.ametys.cms.data.ContentDataHelper; 032import org.ametys.cms.data.ContentValue; 033import org.ametys.cms.data.RichText; 034import org.ametys.cms.repository.ModifiableDefaultContent; 035import org.ametys.odf.cdmfr.CDMEntity; 036import org.ametys.plugins.repository.AmetysRepositoryException; 037import org.ametys.plugins.repository.metadata.UnknownMetadataException; 038 039/** 040 * OrgUnit java object 041 */ 042public class OrgUnit extends ModifiableDefaultContent<OrgUnitFactory> implements CDMEntity 043{ 044 /** code attribute. */ 045 public static final String CODE = "code"; 046 /** prefix for the code attribute */ 047 public static final String CODE_PREFIX = "orgunit-"; 048 /** code RNE attribute. */ 049 public static final String CODE_UAI = "codeUAI"; 050 /** SIRET attribute. */ 051 public static final String SIRET = "siret"; 052 /** acronym attribute. */ 053 public static final String ACRONYM = "acronym"; 054 /** description attribute. */ 055 public static final String DESCRIPTION = "description"; 056 /** admission description attribute. */ 057 public static final String ADMISSION_INFO = "admissionInfo"; 058 /** regulations attribute. */ 059 public static final String REGULATIONS = "regulations"; 060 /** expenses attribute. */ 061 public static final String EXPENSES = "expenses"; 062 /** universal adjustement attribute. */ 063 public static final String UNIVERSAL_ADJUSTMENT = "universalAdjustment"; 064 /** student facilities attribute. */ 065 public static final String STUDENT_FACILITIES = "studentFacilities"; 066 /** additional data attribute. */ 067 public static final String ADDITIONNAL_INFOS = "additionalInfos"; 068 /** web link attribute. */ 069 public static final String WEB_LINK_LABEL = "webLinkLabel"; 070 /** web link url attribute. */ 071 public static final String WEB_LINK_URL = "webLinkUrl"; 072 /** attribute holding the parent orgunit */ 073 public static final String PARENT_ORGUNIT = "parentOrgUnit"; 074 /** attribute holding the contact */ 075 public static final String CONTACTS = "contactsReferences"; 076 /** attribute holding the child orgunits */ 077 public static final String CHILD_ORGUNITS = "childOrgUnits"; 078 /** OrgUnit type */ 079 public static final String TYPE = "type"; 080 081 /** 082 * Constructor 083 * @param node The JCR node 084 * @param parentPath The parent path 085 * @param factory The factory 086 */ 087 public OrgUnit(Node node, String parentPath, OrgUnitFactory factory) 088 { 089 super(node, parentPath, factory); 090 } 091 092 /** 093 * Remove reference from local and remote metadata 094 * @param metadataName The metadata name 095 * @param value The value of reference to remove 096 */ 097 public void removeReference (String metadataName, String value) 098 { 099 List<String> references = ContentDataHelper.getContentIdsListFromMultipleContentData(this, metadataName); 100 if (references.contains(value)) 101 { 102 references.remove(value); 103 setValue(metadataName, references.toArray(new String[references.size()])); 104 } 105 106 String[] altReferences = getMetadataHolder().getStringArray(metadataName + ExternalizableMetadataHelper.ALTERNATIVE_SUFFIX, new String[0]); 107 List<String> remoteList = new ArrayList<>(Arrays.asList(altReferences)); 108 if (remoteList.contains(value)) 109 { 110 remoteList.remove(value); 111 } 112 getMetadataHolder().setMetadata(metadataName + ExternalizableMetadataHelper.ALTERNATIVE_SUFFIX, remoteList.toArray(new String[remoteList.size()])); 113 } 114 115 // --------------------------------------------------------------------------------------// 116 // 117 // SUB ORGUNITS 118 // 119 // --------------------------------------------------------------------------------------// 120 121 /** 122 * Return a List of orgUnits IDs up to date, Each ID is checked to 123 * remove deleted elements 124 * @return List<String> 125 */ 126 public List<String> getSubOrgUnits() 127 { 128 return ContentDataHelper.getContentIdsListFromMultipleContentData(this, CHILD_ORGUNITS); 129 } 130 131 /** 132 * Add a sub orgnit 133 * @param id the sub orgunit id to add 134 */ 135 public void addSubOrgUnit (String id) 136 { 137 String[] orgunits = ArrayUtils.EMPTY_STRING_ARRAY; 138 try 139 { 140 orgunits = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), CHILD_ORGUNITS, ExternalizableMetadataStatus.LOCAL); 141 } 142 catch (UnknownMetadataException e) 143 { 144 // Nothing 145 } 146 147 List<String> orgunitsList = new ArrayList<>(Arrays.asList(orgunits)); 148 orgunitsList.add(id); 149 150 ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), CHILD_ORGUNITS, orgunitsList.toArray(new String[orgunitsList.size()]), ExternalizableMetadataStatus.LOCAL); 151 } 152 153 /** 154 * Delete a sub orgnit 155 * @param id the sub orgunit id to delete 156 */ 157 public void removeSubOrgUnit (String id) 158 { 159 String[] orgunits = ArrayUtils.EMPTY_STRING_ARRAY; 160 try 161 { 162 orgunits = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), CHILD_ORGUNITS, ExternalizableMetadataStatus.LOCAL); 163 } 164 catch (UnknownMetadataException e) 165 { 166 // Nothing 167 } 168 169 List<String> orgunitsList = new ArrayList<>(Arrays.asList(orgunits)); 170 if (orgunitsList.contains(id)) 171 { 172 orgunitsList.remove(id); 173 } 174 175 ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), CHILD_ORGUNITS, orgunitsList.toArray(new String[orgunitsList.size()]), ExternalizableMetadataStatus.LOCAL); 176 } 177 178 /** 179 * Get the id of parent {@link OrgUnit} 180 * @return the id of parent {@link OrgUnit} or null; 181 */ 182 public OrgUnit getParentOrgUnit() 183 { 184 return Optional.ofNullable((ContentValue) getValue(PARENT_ORGUNIT)) 185 .flatMap(ContentValue::getContentIfExists) 186 .map(OrgUnit.class::cast) 187 .orElse(null); 188 } 189 190 // --------------------------------------------------------------------------------------// 191 // 192 // CONTACTS 193 // 194 // --------------------------------------------------------------------------------------// 195 196 /** 197 * Return a List of contact IDs 198 * @return a list of uuid 199 */ 200 public List<String> getContacts() 201 { 202 return ContentDataHelper.getContentIdsListFromMultipleContentData(this, CONTACTS); 203 } 204 205 /** 206 * Return a List of local contact IDs 207 * @return a list of uuid 208 */ 209 public List<String> getLocalContacts() 210 { 211 try 212 { 213 String[] contacts = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), CONTACTS, ExternalizableMetadataStatus.LOCAL); 214 return new ArrayList<>(Arrays.asList(contacts)); 215 } 216 catch (UnknownMetadataException e) 217 { 218 return Collections.EMPTY_LIST; 219 } 220 } 221 222 /** 223 * Return a List of remote contact IDs 224 * @return a list of uuid 225 */ 226 public List<String> getRemoteContacts() 227 { 228 try 229 { 230 String[] contacts = ExternalizableMetadataHelper.getStringArray(getMetadataHolder(), CONTACTS, ExternalizableMetadataStatus.EXTERNAL); 231 return new ArrayList<>(Arrays.asList(contacts)); 232 } 233 catch (UnknownMetadataException e) 234 { 235 return Collections.EMPTY_LIST; 236 } 237 } 238 239 240 // --------------------------------------------------------------------------------------// 241 // 242 // GETTERS AND SETTERS 243 // 244 // --------------------------------------------------------------------------------------// 245 246 /** 247 * Get the code 248 * @return The code 249 * @throws AmetysRepositoryException if failed to get metadata 250 */ 251 public String getCode() throws AmetysRepositoryException 252 { 253 return getValue(CODE, false, StringUtils.EMPTY); 254 } 255 256 /** 257 * Set the code of the {@link OrgUnit} 258 * @param code The code 259 * @throws AmetysRepositoryException if failed to set metadata 260 */ 261 public void setCode(String code) throws AmetysRepositoryException 262 { 263 if (_getFactory()._getSynchronizedMetadata(this).contains(OrgUnit.CODE)) 264 { 265 ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), OrgUnit.CODE, code, ExternalizableMetadataStatus.LOCAL); 266 } 267 else 268 { 269 ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), OrgUnit.CODE, code); 270 } 271 } 272 273 /** 274 * Get the UAI code 275 * @return the UAI code 276 * @throws AmetysRepositoryException if failed to get metadata 277 */ 278 public String getUAICode() throws AmetysRepositoryException 279 { 280 return getValue(CODE_UAI, false, StringUtils.EMPTY); 281 } 282 283 /** 284 * Get the SIRET 285 * @return the SIRET 286 * @throws AmetysRepositoryException if failed to get metadata 287 */ 288 public String getSIRET() throws AmetysRepositoryException 289 { 290 return getValue(SIRET, false, StringUtils.EMPTY); 291 } 292 293 /** 294 * Get the orgunit type 295 * @return the orgunit type 296 * @throws AmetysRepositoryException if failed to get metadata 297 */ 298 public String getType() throws AmetysRepositoryException 299 { 300 return ContentDataHelper.getContentIdFromContentData(this, TYPE); 301 } 302 303 /** 304 * Set the UAI code 305 * @param rne the code 306 * @throws AmetysRepositoryException if failed to set metadata 307 */ 308 public void setUAICode(String rne) throws AmetysRepositoryException 309 { 310 if (_getFactory()._getSynchronizedMetadata(this).contains(OrgUnit.CODE_UAI)) 311 { 312 ExternalizableMetadataHelper.setLocalMetadata(getMetadataHolder(), OrgUnit.CODE_UAI, rne, ExternalizableMetadataStatus.LOCAL); 313 } 314 else 315 { 316 ExternalizableMetadataHelper.setMetadata(getMetadataHolder(), OrgUnit.CODE_UAI, rne); 317 } 318 } 319 320 /** 321 * Return the metadata code 322 * 323 * @return the acronym 324 */ 325 public String getAcronym() 326 { 327 return getValue(OrgUnit.ACRONYM, false, StringUtils.EMPTY); 328 } 329 330 /** 331 * Return the description 332 * @return the description 333 */ 334 public RichText getDescription() 335 { 336 return getValue(OrgUnit.DESCRIPTION); 337 } 338 339 /** 340 * Return the metadata admission_info 341 * @return the admission_info 342 */ 343 public RichText getAdmissionInfo() 344 { 345 return getValue(OrgUnit.ADMISSION_INFO); 346 } 347 348 /** 349 * Return the regulations 350 * @return the regulations 351 */ 352 public RichText getRegulations() 353 { 354 return getValue(OrgUnit.REGULATIONS); 355 } 356 357 /** 358 * Return the metadata Expenses 359 * @return the Expenses 360 */ 361 public RichText getExpenses() 362 { 363 return getValue(OrgUnit.EXPENSES); 364 } 365 366 /** 367 * Return the metadata universalAdjustment 368 * @return the universalAdjustment 369 */ 370 public RichText getUniversalAdjustment() 371 { 372 return getValue(OrgUnit.UNIVERSAL_ADJUSTMENT); 373 } 374 375 /** 376 * Return the metadata student_facilities 377 * 378 * @return the student_facilities 379 */ 380 public RichText getStudentFacilities() 381 { 382 return getValue(OrgUnit.STUDENT_FACILITIES); 383 } 384 385 /** 386 * Return the metadata additionnal_infos 387 * 388 * @return the additionnal_infos 389 */ 390 public RichText getAdditionnalInfos() 391 { 392 return getValue(OrgUnit.ADDITIONNAL_INFOS); 393 } 394 395 /** 396 * Return the metadata web_link 397 * 398 * @return the web_link 399 */ 400 public String getWebLinkLabel() 401 { 402 return getValue(OrgUnit.WEB_LINK_LABEL, false, StringUtils.EMPTY); 403 } 404 405 /** 406 * Get the web link URL 407 * @return the web link URL or null 408 */ 409 public String getWebLinkURL() 410 { 411 return getValue(WEB_LINK_URL, false, StringUtils.EMPTY); 412 } 413 414 // --------------------------------------------------------------------------------------// 415 // CDM-fr 416 // --------------------------------------------------------------------------------------// 417 @Override 418 public String getCDMId() 419 { 420 String cdmCode = getCdmCode(); 421 if (StringUtils.isEmpty(cdmCode)) 422 { 423 return "FRUAI" + _getFactory()._getRootOrgUnitUAI() + "OU" + getUAICode(); 424 } 425 return cdmCode; 426 } 427 428 @Override 429 public String getCdmCode() 430 { 431 return getValue(CDM_CODE, false, StringUtils.EMPTY); 432 } 433 434 @Override 435 public void setCdmCode(String cdmCode) 436 { 437 setValue(CDM_CODE, cdmCode); 438 } 439 440}