| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.business; |
| 9 | |
|
| 10 | |
import java.io.File; |
| 11 | |
import java.io.FileNotFoundException; |
| 12 | |
import java.io.FileOutputStream; |
| 13 | |
import java.io.IOException; |
| 14 | |
import java.rmi.RemoteException; |
| 15 | |
import java.util.Calendar; |
| 16 | |
import java.util.Iterator; |
| 17 | |
import java.util.List; |
| 18 | |
|
| 19 | |
import javax.xml.rpc.holders.BooleanHolder; |
| 20 | |
import javax.xml.rpc.holders.CalendarHolder; |
| 21 | |
import javax.xml.rpc.holders.IntHolder; |
| 22 | |
import javax.xml.rpc.holders.StringHolder; |
| 23 | |
|
| 24 | |
import org.apache.commons.logging.Log; |
| 25 | |
import org.apache.commons.logging.LogFactory; |
| 26 | |
|
| 27 | |
import com.buckosoft.PicMan.PicManService.PicManServiceProxy; |
| 28 | |
import com.buckosoft.PicMan.domain.Pic; |
| 29 | |
import com.buckosoft.PicMan.domain.Root; |
| 30 | |
import com.buckosoft.PicMan.domain.Set; |
| 31 | |
import com.buckosoft.PicMan.domain.SyncLogEntry; |
| 32 | |
import com.buckosoft.PicMan.domain.System; |
| 33 | |
import com.buckosoft.PicMan.util.SetsCRC; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public class SyncManager_Old { |
| 42 | |
public final static int UNKNOWN = 0; |
| 43 | |
public final static int INIT = 1; |
| 44 | |
public final static int DOWN = 2; |
| 45 | |
public final static int ACTIVE = 3; |
| 46 | |
public final static int DONE = 4; |
| 47 | |
public final static int FAILED = 5; |
| 48 | |
|
| 49 | |
|
| 50 | |
private static final boolean DEBUG = true; |
| 51 | |
private static final boolean DEBUGSETS = false; |
| 52 | |
private static final boolean DEBUGSOAP = false; |
| 53 | |
|
| 54 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
| 55 | |
|
| 56 | 0 | private int syncState = UNKNOWN; |
| 57 | |
|
| 58 | |
private String syncUrl; |
| 59 | |
|
| 60 | 0 | private String serviceUri = "PicManService/services/PicManServiceSOAP"; |
| 61 | |
private PicManFacade pmf; |
| 62 | |
|
| 63 | 0 | private PicManServiceProxy pmsp = null; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 0 | public SyncManager_Old() { |
| 68 | 0 | syncState = INIT; |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public void setPicMan(PicManFacade pmf) { |
| 75 | 0 | this.pmf = pmf; |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public int getSyncState() { |
| 82 | 0 | return syncState; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public String getSyncStateAsText() { |
| 89 | 0 | if (!pmf.getDB().getSystem().isSyncEnable()) |
| 90 | 0 | return("Off"); |
| 91 | 0 | switch (syncState) { |
| 92 | 0 | case UNKNOWN: return("??"); |
| 93 | 0 | case INIT: return("Init"); |
| 94 | 0 | case DOWN: return("Down"); |
| 95 | 0 | case ACTIVE: return("Active"); |
| 96 | 0 | case DONE: return("Done"); |
| 97 | 0 | case FAILED: return("Failed"); |
| 98 | |
} |
| 99 | 0 | return("???"); |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public void run() { |
| 105 | |
SyncLogEntry sle; |
| 106 | 0 | this.syncState = ACTIVE; |
| 107 | 0 | String auth = ""; |
| 108 | 0 | System sys = pmf.getDB().getSystem(); |
| 109 | 0 | if (!sys.isSyncEnable()) |
| 110 | 0 | return; |
| 111 | 0 | syncUrl = sys.getSyncUrl(); |
| 112 | 0 | if (!syncUrl.endsWith("/")) |
| 113 | 0 | syncUrl += "/"; |
| 114 | 0 | syncUrl += serviceUri; |
| 115 | |
if (DEBUG) |
| 116 | 0 | logger.info("Contacting: " + syncUrl); |
| 117 | 0 | if (pmsp == null) |
| 118 | 0 | pmsp = new PicManServiceProxy(); |
| 119 | 0 | pmsp.setEndpoint(syncUrl); |
| 120 | 0 | String setsCrc = ""; |
| 121 | |
try { |
| 122 | 0 | setsCrc = pmsp.getSetsCRC(""); |
| 123 | 0 | } catch (RemoteException e) { |
| 124 | 0 | setDown(e, "getSetsCRC"); |
| 125 | |
if (DEBUGSOAP) |
| 126 | |
e.printStackTrace(); |
| 127 | 0 | return; |
| 128 | 0 | } |
| 129 | |
|
| 130 | 0 | List<Set> sets = pmf.getDB().getSets(); |
| 131 | 0 | Iterator<Set> iter = sets.iterator(); |
| 132 | 0 | SetsCRC crc = new SetsCRC(); |
| 133 | 0 | while (iter.hasNext()) { |
| 134 | 0 | Set set = iter.next(); |
| 135 | 0 | crc.addSet(set); |
| 136 | 0 | } |
| 137 | |
if (DEBUG) |
| 138 | 0 | logger.info("Remote Sets CRC = " + setsCrc + " local = " + crc.getCRC()); |
| 139 | 0 | if (!setsCrc.equals("" + crc.getCRC())) { |
| 140 | 0 | synchronizeSets(auth); |
| 141 | |
} |
| 142 | 0 | Pic pic = pmf.getDB().getNewestPic(); |
| 143 | 0 | Calendar cal = Calendar.getInstance(); |
| 144 | 0 | cal.setTime(pic.getDate()); |
| 145 | 0 | String[] ss = null; |
| 146 | |
try { |
| 147 | 0 | ss = pmsp.getPicListNewerThan(auth, cal); |
| 148 | 0 | } catch (RemoteException e) { |
| 149 | 0 | setDown(e, "getPicListNewerThan"); |
| 150 | |
if (DEBUGSOAP) |
| 151 | |
e.printStackTrace(); |
| 152 | 0 | return; |
| 153 | 0 | } |
| 154 | 0 | if (ss != null) { |
| 155 | 0 | java.lang.String picName = ss[0]; |
| 156 | 0 | javax.xml.rpc.holders.IntHolder pid = new IntHolder(); |
| 157 | 0 | javax.xml.rpc.holders.StringHolder name = new StringHolder(); |
| 158 | 0 | javax.xml.rpc.holders.IntHolder rid = new IntHolder(); |
| 159 | 0 | javax.xml.rpc.holders.StringHolder location = new StringHolder(); |
| 160 | 0 | javax.xml.rpc.holders.CalendarHolder picDate = new CalendarHolder(); |
| 161 | 0 | javax.xml.rpc.holders.CalendarHolder timestamp = new CalendarHolder(); |
| 162 | |
|
| 163 | 0 | for (int i=0; i<ss.length; i++) { |
| 164 | |
if (DEBUG) |
| 165 | 0 | logger.info("ss[" + i + "] = " + ss[i]); |
| 166 | 0 | sle = new SyncLogEntry(SyncLogEntry.FETCHPIC, ss[i]); |
| 167 | 0 | pmf.addSyncToLog(sle); |
| 168 | |
|
| 169 | 0 | picName = ss[i]; |
| 170 | |
try { |
| 171 | 0 | pmsp.getPicAttributes(auth, ss[i], pid, name, rid, location, picDate, timestamp); |
| 172 | 0 | } catch (RemoteException e) { |
| 173 | 0 | setDown(e, "getPicAttributes"); |
| 174 | |
if (DEBUGSOAP) |
| 175 | |
e.printStackTrace(); |
| 176 | 0 | return; |
| 177 | 0 | } |
| 178 | |
if (DEBUG) { |
| 179 | 0 | logger.info("Fetching pic " + name.value + " rid:" + rid.value + " loc:" + location.value); |
| 180 | 0 | logger.info(" picDate: '" + picDate.value.getTime().toString() + "' timeStamp: '" |
| 181 | 0 | + timestamp.value.getTime().toString() + "'"); |
| 182 | |
} |
| 183 | |
|
| 184 | 0 | byte[] b = null; |
| 185 | |
try { |
| 186 | 0 | b = pmsp.getPicPic(auth, name); |
| 187 | 0 | } catch (RemoteException e) { |
| 188 | 0 | setDown(e, "getPicPic"); |
| 189 | |
if (DEBUGSOAP) |
| 190 | |
e.printStackTrace(); |
| 191 | 0 | return; |
| 192 | 0 | } |
| 193 | |
|
| 194 | 0 | Root root = pmf.getDB().getRoot(rid.value); |
| 195 | 0 | File toDir = new File(root.getPath() + "/" + location.value); |
| 196 | 0 | if (!toDir.isDirectory()) { |
| 197 | |
try { |
| 198 | 0 | sle = new SyncLogEntry(SyncLogEntry.CREATEDIR, location.value); |
| 199 | 0 | pmf.addSyncToLog(sle); |
| 200 | 0 | toDir.mkdirs(); |
| 201 | 0 | } catch (Exception e) { |
| 202 | 0 | e.printStackTrace(); |
| 203 | 0 | return; |
| 204 | 0 | } |
| 205 | |
} |
| 206 | |
|
| 207 | 0 | File toFile = new File(root.getPath() + "/" + location.value + "/" + picName + ".jpg"); |
| 208 | 0 | FileOutputStream fos = null; |
| 209 | |
try { |
| 210 | 0 | fos = new FileOutputStream(toFile); |
| 211 | 0 | } catch (FileNotFoundException e) { |
| 212 | 0 | setDown(e, "Open"); |
| 213 | |
if (DEBUGSOAP) |
| 214 | |
e.printStackTrace(); |
| 215 | 0 | return; |
| 216 | 0 | } |
| 217 | |
try { |
| 218 | 0 | fos.write(b); |
| 219 | 0 | } catch (IOException e) { |
| 220 | 0 | e.printStackTrace(); |
| 221 | |
try { |
| 222 | 0 | fos.close(); |
| 223 | 0 | } catch (IOException e1) { |
| 224 | 0 | } |
| 225 | 0 | return; |
| 226 | 0 | } |
| 227 | |
try { |
| 228 | 0 | fos.close(); |
| 229 | 0 | } catch (IOException e1) { |
| 230 | 0 | e1.printStackTrace(); |
| 231 | 0 | return; |
| 232 | 0 | } |
| 233 | 0 | toFile.setLastModified(picDate.value.getTimeInMillis()); |
| 234 | 0 | toFile.setReadOnly(); |
| 235 | 0 | pic = new Pic(); |
| 236 | 0 | pic.setName(picName); |
| 237 | 0 | pic.setLocation(location.value); |
| 238 | 0 | pic.setRid(rid.value); |
| 239 | 0 | pic.getDate().setTime(timestamp.value.getTime().getTime()); |
| 240 | 0 | pmf.getDB().addPic(pic); |
| 241 | |
|
| 242 | |
|
| 243 | |
} |
| 244 | |
} |
| 245 | 0 | this.syncState = DONE; |
| 246 | 0 | } |
| 247 | |
|
| 248 | |
private void setDown(RemoteException rex, String where) { |
| 249 | 0 | syncState = DOWN; |
| 250 | 0 | SyncLogEntry sle = new SyncLogEntry(SyncLogEntry.FAILED, where); |
| 251 | 0 | sle.setColor(SyncLogEntry.COLOR_BAD); |
| 252 | 0 | sle.setMessage(rex.getLocalizedMessage()); |
| 253 | 0 | pmf.addSyncToLog(sle); |
| 254 | |
if (DEBUG) |
| 255 | 0 | logger.info("rex:" + this.getSyncStateAsText() + " : " + rex.getLocalizedMessage()); |
| 256 | 0 | } |
| 257 | |
|
| 258 | |
private void setDown(Exception rex, String where) { |
| 259 | 0 | syncState = FAILED; |
| 260 | 0 | SyncLogEntry sle = new SyncLogEntry(SyncLogEntry.FAILED, where); |
| 261 | 0 | sle.setColor(SyncLogEntry.COLOR_BAD); |
| 262 | 0 | sle.setMessage(rex.getLocalizedMessage()); |
| 263 | 0 | pmf.addSyncToLog(sle); |
| 264 | |
if (DEBUG) |
| 265 | 0 | logger.info("rex:" + this.getSyncStateAsText() + " : " + rex.getLocalizedMessage()); |
| 266 | 0 | } |
| 267 | |
|
| 268 | |
private void synchronizeSets(String auth) { |
| 269 | |
int i; |
| 270 | |
if (DEBUG) |
| 271 | 0 | logger.info("Sync Sets"); |
| 272 | 0 | SyncLogEntry sle = new SyncLogEntry(SyncLogEntry.INFO, "Sync Sets"); |
| 273 | 0 | sle.setColor(SyncLogEntry.COLOR_BAD); |
| 274 | 0 | pmf.addSyncToLog(sle); |
| 275 | 0 | List<Set> mySets = pmf.getDB().getSetsClone(); |
| 276 | 0 | int hisCount = -1; |
| 277 | |
try { |
| 278 | 0 | hisCount = pmsp.getSetCount(auth); |
| 279 | 0 | } catch (RemoteException e) { |
| 280 | 0 | setDown(e, "synchronizeSets"); |
| 281 | |
if (DEBUGSOAP) |
| 282 | |
e.printStackTrace(); |
| 283 | 0 | return; |
| 284 | 0 | } |
| 285 | |
if (DEBUGSETS) |
| 286 | |
logger.info("Checking his " + hisCount + " sets"); |
| 287 | 0 | IntHolder sid = new IntHolder(); |
| 288 | 0 | StringHolder name = new StringHolder(); |
| 289 | 0 | StringHolder description = new StringHolder(); |
| 290 | 0 | BooleanHolder active = new BooleanHolder(); |
| 291 | 0 | BooleanHolder metaSet = new BooleanHolder(); |
| 292 | 0 | BooleanHolder microSet = new BooleanHolder(); |
| 293 | 0 | BooleanHolder nanoSet = new BooleanHolder(); |
| 294 | 0 | CalendarHolder editDate = new CalendarHolder(); |
| 295 | |
Set set; |
| 296 | 0 | Calendar cal = Calendar.getInstance(); |
| 297 | 0 | int pushCount = 0; |
| 298 | 0 | int pullCount = 0; |
| 299 | 0 | for (i=1; i<=hisCount; i++) { |
| 300 | |
try { |
| 301 | 0 | sid.value = i; |
| 302 | 0 | if (i == 99) |
| 303 | 0 | logger.info("99"); |
| 304 | 0 | pmsp.getSet(auth, sid, name, description, active, metaSet, microSet, nanoSet, editDate); |
| 305 | 0 | } catch (RemoteException e) { |
| 306 | 0 | setDown(e, "synchronizeSets"); |
| 307 | |
if (DEBUGSOAP) |
| 308 | |
e.printStackTrace(); |
| 309 | 0 | return; |
| 310 | 0 | } |
| 311 | 0 | if (sid.value < 0) { |
| 312 | 0 | setDown(new Exception("sid < 0"), "synchronizeSets"); |
| 313 | 0 | return; |
| 314 | |
} |
| 315 | 0 | set = new Set(); |
| 316 | 0 | set.setSid(sid.value); |
| 317 | 0 | set.setName(name.value); |
| 318 | 0 | set.setDescription(description.value); |
| 319 | 0 | set.setActive(active.value); |
| 320 | 0 | set.setMetaSet(metaSet.value); |
| 321 | 0 | set.setMicroSet(microSet.value); |
| 322 | 0 | set.setNanoSet(nanoSet.value); |
| 323 | 0 | set.setEditDate(editDate.value.getTime()); |
| 324 | 0 | Iterator<Set> iter = mySets.iterator(); |
| 325 | 0 | boolean handled = false; |
| 326 | 0 | while (iter.hasNext()) { |
| 327 | 0 | Set mySet = iter.next(); |
| 328 | 0 | if (mySet.getSid() == set.getSid()) { |
| 329 | 0 | if (mySet.compare(mySet, set) != 0) { |
| 330 | |
if (DEBUGSETS) |
| 331 | |
logger.info("mytime=" + mySet.getEditDate() + " histime=" + set.getEditDate()); |
| 332 | 0 | if (mySet.getEditDate().after(set.getEditDate())) { |
| 333 | |
if (DEBUGSETS) |
| 334 | |
logger.info("got set " + set.getSid() + " \"" + set.getName() + "\" = local newer"); |
| 335 | 0 | cal.setTime(mySet.getEditDate()); |
| 336 | |
try { |
| 337 | 0 | pmsp.putSet(auth, mySet.getSid(), mySet.getName(), mySet.getDescription(), |
| 338 | 0 | mySet.isActive(), mySet.isMetaSet(), mySet.isMicroSet(), mySet.isNanoSet(), |
| 339 | |
cal); |
| 340 | 0 | } catch (RemoteException e) { |
| 341 | 0 | setDown(e, "putSet: " + set.getName()); |
| 342 | 0 | return; |
| 343 | 0 | } |
| 344 | 0 | pushCount++; |
| 345 | 0 | iter.remove(); |
| 346 | 0 | handled = true; |
| 347 | 0 | break; |
| 348 | |
} else { |
| 349 | |
if (DEBUGSETS) |
| 350 | |
logger.info("got set " + set.getSid() + " \"" + set.getName() + "\" = remote newer"); |
| 351 | 0 | pmf.getDB().storeSet(set); |
| 352 | 0 | pullCount++; |
| 353 | 0 | iter.remove(); |
| 354 | 0 | handled = true; |
| 355 | 0 | break; |
| 356 | |
} |
| 357 | |
} else { |
| 358 | |
if (DEBUGSETS) |
| 359 | |
logger.info("got set " + set.getSid() + " \"" + set.getName() + "\" = same"); |
| 360 | 0 | iter.remove(); |
| 361 | 0 | handled = true; |
| 362 | 0 | break; |
| 363 | |
} |
| 364 | |
} |
| 365 | 0 | } |
| 366 | 0 | if (!handled) { |
| 367 | |
if (DEBUGSETS) |
| 368 | |
logger.info("not found: " + set.getSid() + " \"" + set.getName() + "\""); |
| 369 | 0 | pmf.getDB().storeSet(set); |
| 370 | 0 | pullCount++; |
| 371 | |
} |
| 372 | |
} |
| 373 | 0 | for (Set mySet : mySets) { |
| 374 | |
if (DEBUGSETS) |
| 375 | |
logger.info("put new set: " + mySet.getName()); |
| 376 | 0 | cal.setTime(mySet.getEditDate()); |
| 377 | 0 | pushCount++; |
| 378 | |
try { |
| 379 | 0 | pmsp.putSet(auth, mySet.getSid(), mySet.getName(), mySet.getDescription(), |
| 380 | 0 | mySet.isActive(), mySet.isMetaSet(), mySet.isMicroSet(), mySet.isNanoSet(), |
| 381 | |
cal); |
| 382 | 0 | } catch (RemoteException e) { |
| 383 | 0 | setDown(e, "putSet: " + mySet.getName()); |
| 384 | 0 | return; |
| 385 | 0 | } |
| 386 | 0 | } |
| 387 | 0 | if (pushCount != 0 || pullCount != 0) |
| 388 | 0 | sle.setMessage("push: " + pushCount + " pull: " + pullCount); |
| 389 | 0 | } |
| 390 | |
|
| 391 | |
} |