001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *                               ***
008 *
009 *   Community License: GPL 3.0
010 *
011 *   This file is free software: you can redistribute it and/or modify
012 *   it under the terms of the GNU General Public License as published
013 *   by the Free Software Foundation, either version 3 of the License,
014 *   or (at your option) any later version.
015 *
016 *   This file is distributed in the hope that it will be useful, but
017 *   WITHOUT ANY WARRANTY; without even the implied warranty of
018 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019 *   GNU General Public License for more details.
020 *
021 *   You should have received a copy of the GNU General Public License
022 *   along with this program. If not, see <http://www.gnu.org/licenses/>.
023 *
024 *                               ***
025 *
026 *   Available Commercial License: GraniteDS SLA 1.0
027 *
028 *   This is the appropriate option if you are creating proprietary
029 *   applications and you are not prepared to distribute and share the
030 *   source code of your application under the GPL v3 license.
031 *
032 *   Please visit http://www.granitedataservices.com/license for more
033 *   details.
034 */
035package org.granite.client.tide.data;
036
037import java.util.ArrayList;
038import java.util.List;
039
040import org.granite.logging.Logger;
041
042/**
043 *  Holds conflict data when locally changed data is in conflict with data coming from the server
044 * 
045 *  @author William DRAI
046 */
047public class Conflicts {
048    
049    @SuppressWarnings("unused")
050    private static Logger log = Logger.getLogger("org.granite.client.tide.data.Conflicts");
051
052    private EntityManager entityManager;
053    
054    private List<Conflict> conflicts = new ArrayList<Conflict>();
055    
056
057
058    public Conflicts(EntityManager entityManager) {
059        this.entityManager = entityManager;
060    }
061    
062    public void addConflict(Object localEntity, Object receivedEntity, List<String> properties) {
063        Conflict conflict = new Conflict(this, localEntity, receivedEntity, properties);
064        conflicts.add(conflict);
065    }
066    
067    public List<Conflict> getConflicts() {
068        return conflicts;
069    }        
070    
071    public boolean isEmpty() {
072        return conflicts.size() == 0;
073    }
074    
075    public boolean isAllResolved() {
076        for (Conflict c : conflicts) {
077            if (!c.isResolved())
078                return false;
079        }
080        return true;
081    }
082    
083    public void acceptClient(Conflict conflict) {
084        entityManager.acceptConflict(conflict, true);
085    }
086    
087    public void acceptAllClient() {
088        for (Conflict c : conflicts)
089            acceptClient(c);
090    }
091    
092    public void acceptServer(Conflict conflict) {
093        entityManager.acceptConflict(conflict, false);
094    }
095    
096    public void acceptAllServer() {
097        for (Conflict c : conflicts)
098            acceptServer(c);
099    }
100}