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