1 18 19 package sync4j.syncclient.spap; 20 21 import java.security.Principal ; 22 import java.sql.Timestamp ; 23 import java.util.Date ; 24 import java.util.Map ; 25 import java.util.Vector ; 26 27 import sync4j.framework.tools.Base64; 28 29 30 import sync4j.syncclient.common.XMLHashMapParser; 31 import sync4j.syncclient.common.logging.Logger; 32 import sync4j.syncclient.spds.SyncException; 33 import sync4j.syncclient.spds.engine.SyncItem; 34 import sync4j.syncclient.spds.engine.SyncItemImpl; 35 import sync4j.syncclient.spds.engine.SyncItemProperty; 36 import sync4j.syncclient.spds.engine.SyncSource; 37 38 39 54 public class AssetSyncSource implements SyncSource { 55 56 57 59 private String sourceURI; 60 61 67 public void setSourceURI(String sourceURI) { 68 this.sourceURI = sourceURI; 69 } 70 71 74 public String getSourceURI() { 75 return sourceURI; 76 } 77 78 79 private String type; 80 81 87 public void setType(String type) { 88 this.type = type; 89 } 90 91 94 public String getType() { 95 return type; 96 } 97 98 99 private String name; 100 101 107 public void setName(String name) { 108 this.name = name; 109 } 110 111 114 public String getName() { 115 return name; 116 } 117 118 119 121 122 private AssetDAO assetDAO = null; 123 124 private Logger logger = new Logger(); 125 126 128 131 public AssetSyncSource() { 132 if (logger.isLoggable(Logger.DEBUG)) { 133 logger.debug("Calling AssetSyncSource()"); 134 } 135 assetDAO = new AssetDAO(); 136 } 137 138 139 145 public SyncItem[] getAllSyncItems(Principal principal) { 146 if (logger.isLoggable(Logger.DEBUG)) { 147 logger.debug("Calling getAllSyncItems in AssetSyncSource"); 148 } 149 150 Vector assetsList = assetDAO.getAllAsset(); 151 int numAssets = assetsList.size(); 152 153 SyncItem[] syncItems = new SyncItem[numAssets]; 154 155 SyncItem syncItem = null; 156 String key = null; 157 String type = null; 158 String state = null; 159 String xml = null; 160 Map assetValues = null; 161 Asset asset = null; 162 AssetVersion newVersion = null; 163 AssetVersion currentVersion = null; 164 Timestamp timeStamp = null; 165 166 for (int i=0; i<numAssets; i++) { 167 asset = (Asset)assetsList.elementAt(i); 168 key = asset.getId(); 169 state = asset.getState(); 170 timeStamp = asset.getLastUpdate(); 171 172 if (state.equals(Asset.STATE_DELETE)) { 173 continue; 175 } 176 177 syncItem = new SyncItemImpl(this, key, 'N'); 178 179 assetValues = asset.toMap(); 180 newVersion = asset.getNewVersion(); 181 if (newVersion != null) { 182 assetValues.putAll(newVersion.toMap()); 183 } else { 184 currentVersion = asset.getCurrentVersion(); 185 assetValues.putAll(currentVersion.toMap()); 186 } 187 188 syncItem.setProperty( 189 new SyncItemProperty( 190 SyncItem.PROPERTY_BINARY_CONTENT , 191 Base64.encode((XMLHashMapParser.toXML(assetValues)).getBytes()) 192 ) 193 ); 194 195 syncItem.setProperty( 196 new SyncItemProperty(SyncItem.PROPERTY_TIMESTAMP,timeStamp) 197 ); 198 199 syncItems[i] = syncItem; 200 } 201 202 return syncItems; 203 } 204 205 211 public SyncItem[] getDeletedSyncItems(Principal principal, Date since) { 212 if (logger.isLoggable(Logger.DEBUG)) { 213 logger.debug("Calling getDeletedSyncItems in AssetSyncSource"); 214 } 215 216 return new SyncItem[0]; 217 } 218 219 225 public SyncItem[] getNewSyncItems(Principal principal, Date since) { 226 if (logger.isLoggable(Logger.DEBUG)) { 227 logger.debug("Calling getNewSyncItems in AssetSyncSource"); 228 } 229 return new SyncItem[0]; 230 } 231 232 238 public SyncItem[] getUpdatedSyncItems(Principal principal, Date since) { 239 if (logger.isLoggable(Logger.DEBUG)) { 240 logger.debug("Calling getUpdatedSyncItems in AssetSyncSource"); 241 } 242 243 SyncItem[] syncItems = new SyncItem[0]; 244 return syncItems; 245 } 246 247 248 257 public SyncItem setSyncItem(Principal principal, SyncItem syncItem) 258 throws SyncException{ 259 260 if (logger.isLoggable(Logger.DEBUG)) { 261 logger.debug("Calling setSyncItem in AssetSyncSource"); 262 } 263 264 String key = syncItem.getKey().getKeyAsString(); 265 266 Date dateSync = (Date )syncItem.getPropertyValue(SyncItem.PROPERTY_TIMESTAMP); 267 268 269 Timestamp timeSync = new Timestamp (dateSync.getTime()); 270 271 String xml = new String ( 272 Base64.decode( 273 (byte[])syncItem.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT) 274 ) 275 ); 276 277 Map values = XMLHashMapParser.toMap(xml); 279 280 281 Asset asset = null; 283 AssetVersion currentVersion = null; 284 try { 285 asset = assetDAO.getAsset(key); 286 currentVersion = asset.getCurrentVersion(); 287 } 288 catch (AssetManagementException ex) { 289 } 290 291 asset = new Asset(values, true); 293 asset.setId(key); 294 295 if (currentVersion != null) { 298 asset.setCurrentVersion(currentVersion); 299 asset.setState(Asset.STATE_UPDATE); 300 } else { 301 asset.setState(Asset.STATE_NEW); 302 } 303 304 asset.setLastUpdate(timeSync); 305 306 try { 307 assetDAO.setAsset(asset, timeSync); 308 } 309 catch (AssetManagementException ex) { 310 throw new SyncException("Error setting item: " + key, ex); 311 } 312 313 return syncItem; 314 315 } 316 317 324 public void removeSyncItem(Principal principal, SyncItem syncItem) 325 throws SyncException { 326 327 if (logger.isLoggable(Logger.DEBUG)) { 328 logger.debug("Calling removeSyncItem in AssetSyncSource"); 329 } 330 331 String key = syncItem.getKey().getKeyAsString(); 332 333 Date dateSync = (Date )syncItem.getPropertyValue(SyncItem.PROPERTY_TIMESTAMP); 334 335 try { 336 assetDAO.setAssetState(key, "D"); 337 338 341 } 344 catch (AssetManagementException ex) { 345 throw new SyncException("Error remove item '" + key + "'", ex); 346 } 347 } 348 349 350 public void beginSync(int syncMode) { 351 352 } 353 354 public void beginSync() { 355 356 } 357 358 public void commitSync() { 359 360 } 361 } | Popular Tags |