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.report.impl.mcc; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import org.apache.commons.lang.StringUtils; 022 023import org.ametys.cms.content.compare.ContentComparatorResult; 024import org.ametys.odf.orgunit.OrgUnit; 025import org.ametys.plugins.repository.AmetysObject; 026 027/** 028 * Structure representing a tree of {@link AmetysObject} to be used during the 029 * processing of the MCC reports. 030 */ 031public class MCCAmetysObjectTree 032{ 033 private MCCAmetysObjectTree _parent; 034 private AmetysObject _current; 035 private List<MCCAmetysObjectTree> _children = new ArrayList<>(); 036 private ContentComparatorResult _change; 037 private String _path; 038 039 private MCCAmetysObjectTree(MCCAmetysObjectTree parent, AmetysObject current, ContentComparatorResult change) 040 { 041 _parent = parent; 042 _current = current; 043 _change = change; 044 } 045 046 private MCCAmetysObjectTree(MCCAmetysObjectTree parent, AmetysObject current) 047 { 048 this(parent, current, null); 049 } 050 051 /** 052 * The constructor 053 * @param current The {@link AmetysObject} the tree is build on 054 */ 055 public MCCAmetysObjectTree(AmetysObject current) 056 { 057 this(null, current); 058 } 059 060 /** 061 * Declare a child for the current object. 062 * @param child child for this node 063 * @return The corresponding {@link MCCAmetysObjectTree} for the child 064 */ 065 public MCCAmetysObjectTree addChild(AmetysObject child) 066 { 067 return addChild(child, null); 068 } 069 070 /** 071 * Declare a child for the current object. 072 * 073 * @param child child for this node 074 * @param change changes for this child (if applicable) 075 * @return The corresponding {@link MCCAmetysObjectTree} for the child 076 */ 077 public MCCAmetysObjectTree addChild(AmetysObject child, ContentComparatorResult change) 078 { 079 MCCAmetysObjectTree childTree = _createChild(child, change); 080 _addChild(childTree); 081 return childTree; 082 } 083 084 /** 085 * Add a new child tree 086 * @param childTree The child tree to add 087 */ 088 protected void _addChild(MCCAmetysObjectTree childTree) 089 { 090 _children.add(childTree); 091 } 092 093 /** 094 * Create a child for this node, without adding it to the tree (usefull for tests before adding, addChild to add) 095 * @param child The child {@link AmetysObject} 096 * @param change Changes for this child (if applicable) 097 * @return A child tree 098 */ 099 private MCCAmetysObjectTree _createChild(AmetysObject child, ContentComparatorResult change) 100 { 101 return new MCCAmetysObjectTree(this, child, change); 102 } 103 104 /** 105 * Get the parent ametys object 106 * @return {@link MCCAmetysObjectTree} or null 107 */ 108 public MCCAmetysObjectTree getParent() 109 { 110 return _parent; 111 } 112 113 /** 114 * Get the current ametys object (root level of the tree). 115 * @return {@link AmetysObject} 116 */ 117 public AmetysObject getCurrent() 118 { 119 return _current; 120 } 121 122 /** 123 * Get the current ametys object (root level of the tree). 124 * @return {@link List} of {@link MCCAmetysObjectTree} 125 */ 126 public List<MCCAmetysObjectTree> getChildren() 127 { 128 return _children; 129 } 130 131 /** 132 * Get the change informations for this node 133 * @return {@link ContentComparatorResult} or null if not applicable 134 */ 135 public ContentComparatorResult getChange() 136 { 137 return _change; 138 } 139 140 /** 141 * Get the path in the tree. 142 * orgunits are excluded. 143 * @return the path as a {@link String} 144 */ 145 public String getPath() 146 { 147 if (_path == null) 148 { 149 if (_parent != null) 150 { 151 _path = _parent.getPath(); 152 } 153 154 // Do not add orgunit to path 155 if (!(_current instanceof OrgUnit)) 156 { 157 String name = _current.getName(); 158 if (StringUtils.isEmpty(_path)) 159 { 160 _path = name; 161 } 162 else 163 { 164 _path += (!StringUtils.endsWith(name, "/") ? '/' : "") + name; 165 } 166 } 167 168 if (_path == null) 169 { 170 _path = StringUtils.EMPTY; 171 } 172 } 173 174 return _path; 175 } 176} 177