001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.service.impl;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.commons.lang.StringUtils;
025import org.codehaus.jackson.map.annotate.JsonDeserialize;
026import org.nuxeo.drive.service.FileSystemChangeSummary;
027import org.nuxeo.drive.service.FileSystemItemChange;
028import org.nuxeo.ecm.core.api.IdRef;
029
030/**
031 * Default implementation of a {@link FileSystemChangeSummary}.
032 *
033 * @author Antoine Taillefer
034 */
035public class FileSystemChangeSummaryImpl implements FileSystemChangeSummary {
036
037    private static final long serialVersionUID = 1L;
038
039    protected List<FileSystemItemChange> fileSystemChanges;
040
041    protected Long syncDate = null;
042
043    protected Long upperBound = null;
044
045    protected Boolean hasTooManyChanges = Boolean.FALSE;
046
047    protected String activeSynchronizationRootDefinitions;
048
049    public FileSystemChangeSummaryImpl() {
050        // Needed for JSON deserialization
051    }
052
053    public FileSystemChangeSummaryImpl(List<FileSystemItemChange> fileSystemChanges,
054            Map<String, Set<IdRef>> activeRootRefs, Long syncDate, Long upperBound, Boolean tooManyChanges) {
055        this.fileSystemChanges = fileSystemChanges;
056        this.syncDate = syncDate;
057        this.upperBound = upperBound;
058        this.hasTooManyChanges = tooManyChanges;
059        List<String> rootDefinitions = new ArrayList<String>();
060        for (Map.Entry<String, Set<IdRef>> entry : activeRootRefs.entrySet()) {
061            for (IdRef ref : entry.getValue()) {
062                rootDefinitions.add(String.format("%s:%s", entry.getKey(), ref.toString()));
063            }
064        }
065        this.activeSynchronizationRootDefinitions = StringUtils.join(rootDefinitions, ",");
066    }
067
068    @Override
069    public List<FileSystemItemChange> getFileSystemChanges() {
070        return fileSystemChanges;
071    }
072
073    @Override
074    @JsonDeserialize(using = FileSystemItemChangeListDeserializer.class)
075    public void setFileSystemChanges(List<FileSystemItemChange> changes) {
076        this.fileSystemChanges = changes;
077    }
078
079    /**
080     * @return the time code of current sync operation in milliseconds since 1970-01-01 UTC rounded to the second as
081     *         measured on the server clock.
082     */
083    @Override
084    public Long getSyncDate() {
085        return syncDate;
086    }
087
088    /**
089     * @return the last available log id in the audit log table
090     */
091    @Override
092    public Long getUpperBound() {
093        return upperBound;
094    }
095
096    @Override
097    public String getActiveSynchronizationRootDefinitions() {
098        return activeSynchronizationRootDefinitions;
099    }
100
101    @Override
102    public void setActiveSynchronizationRootDefinitions(String activeSynchronizationRootDefinitions) {
103        this.activeSynchronizationRootDefinitions = activeSynchronizationRootDefinitions;
104    }
105
106    @Override
107    public void setSyncDate(Long syncDate) {
108        this.syncDate = syncDate;
109    }
110
111    @Override
112    public void setUpperBound(Long upperBound) {
113        this.upperBound = upperBound;
114    }
115
116    @Override
117    public void setHasTooManyChanges(Boolean hasTooManyChanges) {
118        this.hasTooManyChanges = hasTooManyChanges;
119    }
120
121    @Override
122    public Boolean getHasTooManyChanges() {
123        return this.hasTooManyChanges;
124    }
125
126    @Override
127    public String toString() {
128        StringBuilder sb = new StringBuilder(getClass().getSimpleName());
129        sb.append("(");
130        sb.append(String.format("upperBound=%d, ", getUpperBound()));
131        sb.append(String.format("syncDate=%d, ", getSyncDate()));
132        if (hasTooManyChanges) {
133            sb.append("hasTooManyChanges=true");
134        } else {
135            sb.append(String.format("items=[%s]", StringUtils.join(fileSystemChanges, ", ")));
136        }
137        sb.append(")");
138        return sb.toString();
139    }
140}