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