Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / tools / patch / PatchGeneratorImpl.java @ 9512

History | View | Annotate | Download (6.17 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright (c) 2007-2024 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.tools.patch;
24

    
25
import java.util.Iterator;
26
import org.gvsig.tools.ToolsLocator;
27
import org.gvsig.tools.i18n.I18nManager;
28
import org.gvsig.tools.task.SimpleTaskStatus;
29
import org.gvsig.tools.task.UserCancelTaskException;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
/**
34
 *
35
 * @author fdiaz
36
 */
37
class PatchGeneratorImpl<T> implements PatchGenerator<T> {
38
    
39
    public static final Logger LOGGER = LoggerFactory.getLogger(PatchGeneratorImpl.class);
40

    
41
    private final PatchHandler handler;
42

    
43
    PatchGeneratorImpl(PatchHandler handler) {
44
        this.handler = handler;
45
    }
46

    
47
    @Override
48
    public void generate(Iterator<T> r, Iterator<T> l, SimpleTaskStatus status) {
49
        I18nManager i18n = ToolsLocator.getI18nManager();
50
        if (status == null) {
51
            status = ToolsLocator.getTaskStatusManager().createDefaultSimpleTaskStatus(i18n.getTranslation("_Generating_patch"));
52
            status.setAutoremove(true);
53
            status.add();
54
        } else {
55
            status.push();
56
        }
57

    
58
        try {
59
            T remoteItem = null;
60
            T localItem = null;
61

    
62
            if (r.hasNext()) {
63
                remoteItem = r.next();
64
            }
65
            if (l.hasNext()) {
66
                localItem = l.next();
67
            }
68

    
69
            while (true) {
70
                if (status.isCancellationRequested()) {
71
                    throw new UserCancelTaskException();
72
                }
73
                if (localItem == null) {
74
                    if (remoteItem == null) {
75
                        break;
76
                    } else {
77
                        //nuevo en REMOTO
78
                        handler.addRemoteChangeInsert(remoteItem);
79
                        if (r.hasNext()) {
80
                            remoteItem = r.next();
81
                        } else {
82
                            remoteItem = null;
83
                        }
84
                    }
85
                } else if (handler.isNew(localItem)) {
86
                    //nuevo en LOCAL, do nothing
87
                    if (l.hasNext()) {
88
                        localItem = l.next();
89
                    } else {
90
                        localItem = null;
91
                    }
92
                } else if (remoteItem == null || (remoteItem != null && handler.compareId(remoteItem, localItem) > 0)) {
93
                    //Hay un borrado remoto, 
94
                    if (handler.isLocalChange(localItem)) {
95
                        handler.addRemoteChangeDelete(localItem, true);
96
                    } else {
97
                        handler.addRemoteChangeDelete(localItem, false);
98
                    }
99
                    if (l.hasNext()) {
100
                        localItem = l.next();
101
                    } else {
102
                        localItem = null;
103
                    }
104
                } else if (remoteItem != null && handler.compareId(remoteItem, localItem) < 0) {
105
                    //Hay un borrado local o un insert remoto
106
                    if (handler.isLocalChange(remoteItem)) {
107
                        //Do nothing
108
                    } else {
109
                        handler.addRemoteChangeInsert(remoteItem);
110
                    }
111
                    
112
                    if (r.hasNext()) {
113
                        remoteItem = r.next();
114
                    } else {
115
                        remoteItem = null;
116
                    }
117
                } else if (remoteItem != null) { // && handler.compare(remoteItem, localItem)!=0) { //same element, but changed
118
                    if (handler.isLocalChange(localItem)) {
119
                        if (handler.isRemoteChange(remoteItem, localItem)) {
120
                            //conflicto
121
                            handler.addRemoteChangeModify(remoteItem, true);
122
                        } else {
123
                            //Hay modificaci?n local, do nothing
124
                        }
125
                    } else {
126
                        if (handler.isRemoteChange(remoteItem, localItem)) {
127
                            //cambio remoto sin conflicto
128
                            handler.addRemoteChangeModify(remoteItem, false);
129
                        } else {
130
                            //No es modificacion ni en local ni en remoto, no ha cambios. Do nothing
131
                        }
132
                    }
133
                    if (l.hasNext()) {
134
                        localItem = l.next();
135
                    } else {
136
                        localItem = null;
137
                    }
138
                    if (r.hasNext()) {
139
                        remoteItem = r.next();
140
                    } else {
141
                        remoteItem = null;
142
                    }
143
                } else {
144
                    if (l.hasNext()) {
145
                        localItem = l.next();
146
                    } else {
147
                        localItem = null;
148
                    }
149
                    if (r.hasNext()) {
150
                        remoteItem = r.next();
151
                    } else {
152
                        remoteItem = null;
153
                    }
154

    
155
                }
156
            }
157
            status.terminate();
158
        } catch (UserCancelTaskException ex) {
159
            status.cancel();
160
            throw ex;
161
        } catch (Exception ex) {
162
            status.abort();
163
            throw ex;
164
        } finally {
165
            status.pop();
166
        }
167
    }
168

    
169
}