001/* 002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * ${user} 018 * 019 * $$Id: SummaryEntry.java 28482 2008-01-04 15:33:39Z sfermigier $$ 020 */ 021 022/** 023 * @author <a href="[email protected]">Brice Chaffangeon</a> 024 * 025 */ 026package org.nuxeo.ecm.webapp.clipboard; 027 028import java.io.Serializable; 029import java.text.DateFormat; 030import java.text.SimpleDateFormat; 031import java.util.ArrayList; 032import java.util.Collections; 033import java.util.Date; 034import java.util.List; 035import java.util.Calendar; 036 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.core.api.DocumentRef; 039import org.nuxeo.ecm.core.api.PropertyException; 040 041/** 042 * An entry present in a Summary. Each entry has a parent, except for the root entry whose parent is null. 043 * <p> 044 * Note that {@code SummaryEntry.getPath()} can be different than {@code DocumentModel.getPath()} since a DocumentModel 045 * object can be added many times at different level of the working list. 046 * 047 * @author <a href="[email protected]">Brice Chaffangeon</a> 048 */ 049public class SummaryEntry implements Comparable<SummaryEntry>, Serializable { 050 051 private static final long serialVersionUID = -9090607163794413025L; 052 053 private String marker = " "; 054 055 private String bullet = "* "; 056 057 private String pathSeparator = "/"; 058 059 private String fileSeparator = " -> "; 060 061 private String versionSepartor = "_"; 062 063 private String dateSeparator = " - "; 064 065 private String cr = "\n"; 066 067 private String uuid; 068 069 private DocumentRef documentRef; 070 071 private String title; 072 073 private String modifiedDate; 074 075 private String filename; 076 077 private String version; 078 079 private SummaryEntry parent; 080 081 // Not used? 082 public SummaryEntry(String uuid, String title, String modifiedDate, String filename, String version) { 083 this.uuid = uuid; 084 this.title = title; 085 this.modifiedDate = modifiedDate; 086 this.filename = filename; 087 this.version = version; 088 } 089 090 // Used in ClipBoardActionBean 091 public SummaryEntry(String uuid, String title, Date modifiedDate, String filename, String version, 092 SummaryEntry parent) { 093 this.uuid = uuid; 094 this.title = title; 095 if (modifiedDate != null) { 096 this.modifiedDate = getDateFormat().format(modifiedDate); 097 } 098 this.filename = filename; 099 this.version = version; 100 this.parent = parent; 101 } 102 103 // Used in ClipBoardActionBean 104 public SummaryEntry(DocumentModel doc) { 105 uuid = doc.getRef().toString(); 106 try { 107 title = (String) doc.getProperty("dublincore", "title"); 108 } catch (PropertyException e) { 109 title = null; 110 } 111 documentRef = doc.getRef(); 112 113 Object major; 114 try { 115 major = doc.getProperty("uid", "major_version"); 116 } catch (PropertyException e) { 117 major = null; 118 } 119 Object minor; 120 try { 121 minor = doc.getProperty("uid", "minor_version"); 122 } catch (PropertyException e) { 123 minor = null; 124 } 125 Object date; 126 try { 127 date = doc.getProperty("dublincore", "modified"); 128 } catch (PropertyException e) { 129 date = null; 130 } 131 132 if (major != null && minor != null) { 133 version = major.toString() + '.' + minor.toString(); 134 } 135 136 if (date != null) { 137 modifiedDate = getDateFormat().format(((Calendar) date).getTime()); 138 } 139 try { 140 filename = (String) doc.getProperty("file", "filename"); 141 } catch (PropertyException e) { 142 filename = null; 143 } 144 } 145 146 public SummaryEntry(DocumentRef reference) { 147 documentRef = reference; 148 } 149 150 public SummaryEntry() { 151 } 152 153 public static DateFormat getDateFormat() { 154 // not thread-safe so don't use a static instance 155 return new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 156 } 157 158 public String getUuid() { 159 return uuid; 160 } 161 162 public void setUuid(String uuid) { 163 this.uuid = uuid; 164 } 165 166 public String getTitle() { 167 return title; 168 } 169 170 public void setTitle(String title) { 171 this.title = title; 172 } 173 174 public String getModifiedDate() { 175 return modifiedDate; 176 } 177 178 public void setModifiedDate(String modifiedDate) { 179 this.modifiedDate = modifiedDate; 180 } 181 182 public String getFilename() { 183 return filename; 184 } 185 186 public void setFilename(String filename) { 187 this.filename = filename; 188 } 189 190 public String getVersion() { 191 return version; 192 } 193 194 public void setVersion(String version) { 195 this.version = version; 196 } 197 198 /** 199 * @return the entry for a flat view 200 */ 201 public String toFlatString() { 202 StringBuilder sb = new StringBuilder(); 203 204 sb.append(cr); 205 sb.append(marker); 206 207 sb.append(getPath()); 208 209 if (filename != null && !"".equals(filename)) { 210 sb.append(fileSeparator); 211 sb.append(filename); 212 } 213 if (version != null && !"".equals(version)) { 214 sb.append(versionSepartor); 215 sb.append(version); 216 } 217 if (modifiedDate != null && !"".equals(modifiedDate)) { 218 sb.append(dateSeparator); 219 sb.append(modifiedDate); 220 } 221 222 return sb.toString(); 223 } 224 225 /** 226 * @return the entry for a hierarchical view 227 */ 228 public String toTreeString() { 229 StringBuilder sb = new StringBuilder(); 230 231 sb.append(cr); 232 sb.append(marker); 233 sb.append(bullet); 234 sb.append(title); 235 236 if (filename != null && !"".equals(filename)) { 237 sb.append(fileSeparator); 238 sb.append(filename); 239 } 240 if (version != null && !"".equals(version)) { 241 sb.append(versionSepartor); 242 sb.append(version); 243 } 244 if (modifiedDate != null && !"".equals(modifiedDate)) { 245 sb.append(dateSeparator); 246 sb.append(modifiedDate); 247 } 248 return sb.toString(); 249 } 250 251 @Override 252 public String toString() { 253 return toFlatString(); 254 } 255 256 public SummaryEntry getParent() { 257 return parent; 258 } 259 260 public void setParent(SummaryEntry parent) { 261 this.parent = parent; 262 } 263 264 public void setParent(DocumentRef parentRef) { 265 parent = new SummaryEntry(parentRef); 266 } 267 268 public DocumentRef getDocumentRef() { 269 return documentRef; 270 } 271 272 public void setDocumentRef(DocumentRef documentRef) { 273 this.documentRef = documentRef; 274 } 275 276 @Override 277 public boolean equals(Object obj) { 278 if (this == obj) { 279 return true; 280 } 281 if (!(obj instanceof SummaryEntry)) { 282 return false; 283 } 284 return documentRef.equals(((SummaryEntry) obj).documentRef) && getPath().equals(((SummaryEntry) obj).getPath()); 285 } 286 287 @Override 288 public int hashCode() { 289 return getPath().hashCode(); 290 } 291 292 public boolean hasParent() { 293 return parent != null; 294 } 295 296 /** 297 * Calls itself recursively to build a list with all entry of the path. 298 * 299 * @param pathList the list where to add the found path item 300 * @param parentEntry is the SummaryEntry to watch 301 * @return all entry in the path for a given entry (parentEntry). 302 */ 303 public static List<SummaryEntry> getPathList(List<SummaryEntry> pathList, SummaryEntry parentEntry) { 304 if (pathList == null) { 305 pathList = new ArrayList<SummaryEntry>(); 306 } 307 if (parentEntry != null) { 308 pathList.add(parentEntry); 309 if (parentEntry.parent != null) { 310 getPathList(pathList, parentEntry.parent); 311 } 312 } 313 314 return pathList; 315 } 316 317 /** 318 * Returns something like /Workspace1/Folder1/File1. Is not tied to DocumentModel.getPath() 319 * 320 * @return the full path of the SummaryEntry in the summary. 321 */ 322 public String getPath() { 323 StringBuilder sb = new StringBuilder(); 324 List<SummaryEntry> pathList = getPathList(null, this); 325 Collections.reverse(pathList); 326 int i = 0; 327 for (SummaryEntry entry : pathList) { 328 sb.append(entry.title); 329 if (i < pathList.size() - 1) { 330 sb.append('/'); 331 } 332 i++; 333 } 334 335 return sb.toString(); 336 } 337 338 public int compareTo(SummaryEntry o) { 339 if (o != null) { 340 return getPath().compareTo(o.getPath()); 341 } 342 return 0; 343 } 344 345 public String getMarker() { 346 return marker; 347 } 348 349 public void setMarker(String marker) { 350 this.marker = marker; 351 } 352 353 public String getBullet() { 354 return bullet; 355 } 356 357 public void setBullet(String bullet) { 358 this.bullet = bullet; 359 } 360 361 public String getCr() { 362 return cr; 363 } 364 365 public void setCr(String cr) { 366 this.cr = cr; 367 } 368 369 public String getPathSeparator() { 370 return pathSeparator; 371 } 372 373 public void setPathSeparator(String pathSeparator) { 374 this.pathSeparator = pathSeparator; 375 } 376 377 public String getFileSeparator() { 378 return fileSeparator; 379 } 380 381 public void setFileSeparator(String fileSeparator) { 382 this.fileSeparator = fileSeparator; 383 } 384 385 public String getVersionSepartor() { 386 return versionSepartor; 387 } 388 389 public void setVersionSepartor(String versionSepartor) { 390 this.versionSepartor = versionSepartor; 391 } 392 393 public String getDateSeparator() { 394 return dateSeparator; 395 } 396 397 public void setDateSeparator(String dateSeparator) { 398 this.dateSeparator = dateSeparator; 399 } 400 401}