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