View Javadoc

1   package org.opensync.engine.server;
2   
3   import java.io.*;
4   import java.util.*;
5   import java.text.*;
6   
7   import org.opensync.engine.server.Adapter;
8   import org.opensync.engine.server.adapter.*;
9   import org.opensync.engine.server.View;
10  
11  /***
12   * This object represents a synchronization
13   *
14   * @version	1.0
15   * @author	SOFTMED
16   */
17  
18  public class Synchronization implements Serializable {
19    /***
20     * The synchronization is not executed
21     *
22     */
23    public final static int STATUS_NOT_EXDECUTED = 0;
24    /***
25     * The synchronization is running
26     *
27     */
28    public final static int STATUS_RUNNING = 1;
29    /***
30     * The synchronization succed
31     *
32     */
33    public final static int STATUS_SUCCESS = 2;
34    /***
35     * The synchronization failed
36     *
37     */
38    public final static int STATUS_FAIL = 3;
39    /***
40     * The name of the Synchronization
41     *
42     */
43    private String name;
44    /***
45     * The url of the mapping file
46     *
47     */
48    private String urlMapping;
49    /***
50     * The form source
51     *
52     *
53     */
54    private Source fromSource;
55    /***
56     * The to source
57     *
58     */
59    private Source toSource;
60    /***
61     * The from source
62     *
63     */
64    private View fromView;
65    /***
66     * The to view
67     *
68     */
69    private View toView;
70    /***
71     * The date of the last execution
72     *
73     */
74    private Date lastDone;
75    /***
76     * The status of the last execution
77     *
78     */
79    private int lastStatus;
80  
81    /***
82     * Construct a Synchronization object
83     *
84     */
85    public Synchronization() {
86    }
87    /***
88     * Set the name of the synchronization
89     *
90     * @param	name
91     */
92    public void setName(String name) {
93      this.name = name;
94    }
95    /***
96     * Get the name of the synchronization
97     *
98     */
99    public String getName() {
100     return name;
101   }
102   /***
103    * Set the from source
104    *
105    * @param	fromSource the from source
106    */
107   public void setFromSource(Source fromSource) {
108     this.fromSource = fromSource;
109   }
110   /***
111    * Get the from source
112    *
113    */
114   public Source getFromSource() {
115     return fromSource;
116   }
117   /***
118    * Set the from view
119    *
120    * @param	fromView the from view
121    */
122   public void setFromView(View fromView) {
123     this.fromView = fromView;
124   }
125   /***
126    * Get the from view
127    *
128    */
129   public View getFromView() {
130     return fromView;
131   }
132   /***
133    * Set the to source
134    *
135    * @param	toSource the to source
136    */
137   public void setToSource(Source toSource) {
138     this.toSource = toSource;
139   }
140   /***
141    * Get the to source
142    *
143    */
144   public Source getToSource() {
145     return toSource;
146   }
147   /***
148    * Set the to view
149    *
150    * @param	toView the to view
151    */
152   public void setToView(View toView) {
153     this.toView = toView;
154   }
155   /***
156    * Get the to view
157    *
158    */
159   public View getToView() {
160     return toView;
161   }
162   /***
163    * Set the url of the mapping file
164    *
165    * @param	urlMapping the url of the mapping file
166    */
167   public void setUrlMapping(String urlMapping) {
168     this.urlMapping = urlMapping;
169   }
170   /***
171    * Get the url of the mapping file
172    *
173    */
174   public String getUrlMapping() {
175     return urlMapping;
176   }
177   /***
178    * Get the date of the last execution
179    *
180    */
181   public Date getLastDone() {
182     return lastDone;
183   }
184   /***
185    * Set the status of the las execution
186    *
187    * @param	lastStatus the status of the last execution
188    */
189   public void setLastStatus(int lastStatus) {
190     this.lastStatus = lastStatus;
191   }
192   /***
193    * Get the status of the last execution
194    *
195    */
196   public int getLastStatus() {
197     return lastStatus;
198   }
199 
200   public long lastModified() throws OpenSyncException, IOException {
201     String fileSeparator = System.getProperty("file.separator");
202     String configFolder = System.getProperty("opensync.configfolder");
203     configFolder = (configFolder == null ? "" : configFolder);
204 
205     long lastModified = -1;
206 
207     String path = OpenSync.getInstance().getDirectoryPath("etc"+fileSeparator+configFolder+fileSeparator+"mapping", true);
208     File file = new File(path);
209     if (!(file.exists() && file.isDirectory())){
210       throw new OpenSyncException("error.directory.not-found",new Object[]{path});
211     }
212     lastModified = new File(path + fileSeparator + this.getUrlMapping()).lastModified();
213     //System.out.println("Synchro. "+path + "/" + this.getUrlMapping()+" : "+lastModified);
214 
215     lastModified = Math.max(lastModified, getFromSource().lastModified());
216     lastModified = Math.max(lastModified, getToSource().lastModified());
217 
218     Adapter anAdapter = getFromSource().getConnector().getAdapter();
219     String descriptorsPath = "";
220     if (anAdapter instanceof BDAdapter) {
221       descriptorsPath = "etc"+fileSeparator+configFolder+fileSeparator+"query";
222     } else {
223       descriptorsPath = "etc"+fileSeparator+configFolder+fileSeparator+"descript";
224     }
225     Protocol protocol = this.getFromSource().getConnector().getProtocol();
226     if (protocol != null) {
227       lastModified = Math.max(lastModified, getFromView().lastModifiedFrom(descriptorsPath, protocol.getFolder()));
228     }
229     anAdapter = getToSource().getConnector().getAdapter();
230     descriptorsPath = "";
231     if (anAdapter instanceof BDAdapter) {
232       descriptorsPath = "etc"+fileSeparator+configFolder+fileSeparator+"query";
233     } else {
234       descriptorsPath = "etc"+fileSeparator+configFolder+fileSeparator+"descript";
235     }
236     lastModified = Math.max(lastModified, getToView().lastModifiedTo(descriptorsPath));
237 
238     return lastModified;
239   }
240 
241   /***
242    * Use for debug only
243    *
244    */
245   public String toString(){
246     SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
247     StringBuffer buffer = new StringBuffer();
248     buffer.append("Synchronization\n{");
249     buffer.append("\tname : ").append(name).append("\n");
250     buffer.append("\tfromSource : ").append(fromSource.getName()).append("\n");
251     buffer.append("\tfromView : ").append(fromView.getName()).append("\n");
252     buffer.append("\ttoSource : ").append(toSource.getName()).append("\n");
253     buffer.append("\ttoView : ").append(toView.getName()).append("\n");
254     buffer.append("\turlMapping : ").append(urlMapping).append("\n");
255     buffer.append("\tlastDone : ").append(
256       lastDone != null ? dateFormat.format(lastDone) : ""
257     ).append("\n");
258     buffer.append("}\n");
259     return buffer.toString();
260   }
261   /***
262    * Se the date of the last execution
263    *
264    * @param	lastDone the date of the last execution
265    */
266   public void setLastDone(Date lastDone) {
267     this.lastDone = lastDone;
268   }
269 }