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