001: package org.outerj.daisy.sync.service;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007: import java.util.TreeMap;
008:
009: import org.outerj.daisy.repository.VariantKey;
010: import org.outerj.daisy.sync.Entity;
011: import org.outerj.daisy.sync.EntityNotFoundException;
012: import org.outerj.daisy.sync.SyncState;
013: import org.outerj.daisy.sync.Synchronizer;
014: import org.outerj.daisy.sync.SystemState;
015: import org.outerj.daisy.sync.dao.InternalEntityDao;
016: import org.outerj.daisy.sync.dao.Locker;
017: import org.outerj.daisy.sync.dao.SyncEntityDao;
018:
019: public class SyncServiceImpl implements SyncService {
020: private SyncEntityDao syncEntityDao;
021:
022: private InternalEntityDao daisyEntityDao;
023:
024: private Synchronizer synchronizer;
025:
026: private Locker locker;
027:
028: public SyncServiceImpl(Synchronizer synchronizer) {
029: this .synchronizer = synchronizer;
030: this .syncEntityDao = synchronizer.getSyncDao();
031: this .daisyEntityDao = synchronizer.getDaisyDao();
032: this .locker = synchronizer.getLocker();
033: }
034:
035: public Map<String, List<Entity>> getConflicts() {
036: return entityListToMap(syncEntityDao
037: .getEntitiesByState(SyncState.CONFLICT));
038: }
039:
040: public Entity getDaisyEntity(String documentId, long branchId,
041: long languageId) throws EntityNotFoundException {
042: VariantKey key = new VariantKey(documentId, branchId,
043: languageId);
044: return daisyEntityDao.getEntity(key);
045: }
046:
047: public Entity getSyncEntity(String documentId, long branchId,
048: long languageId) throws EntityNotFoundException {
049: VariantKey key = new VariantKey(documentId, branchId,
050: languageId);
051: return syncEntityDao.getEntity(key);
052: }
053:
054: public Map<String, List<Entity>> getDaisyDeletes() {
055: return entityListToMap(syncEntityDao.getDaisyDeletedEntities());
056: }
057:
058: public Map<String, List<Entity>> getDaisyOnlys() {
059: return entityListToMap(syncEntityDao
060: .getEntitiesByState(SyncState.DSY_ONLY));
061: }
062:
063: public Map<String, List<Entity>> getPermanentDaisyOverrules() {
064: return entityListToMap(syncEntityDao
065: .getEntitiesByState(SyncState.CONFLICT_DSY_RULES));
066: }
067:
068: private Map<String, List<Entity>> entityListToMap(
069: List<Entity> entities) {
070: Map<String, List<Entity>> nameEntityMap = new TreeMap<String, List<Entity>>();
071: for (Entity entity : entities) {
072: List<Entity> list = nameEntityMap.get(entity
073: .getInternalName());
074: if (list == null) {
075: list = new ArrayList<Entity>();
076: nameEntityMap.put(entity.getInternalName(), list);
077: }
078: list.add(entity);
079: }
080: return nameEntityMap;
081: }
082:
083: public void resolveConflict(String documentId, long branchId,
084: long languageId, SyncState resolution) throws Exception {
085: VariantKey key = new VariantKey(documentId, branchId,
086: languageId);
087: Entity entity = syncEntityDao.getEntity(key);
088: if (entity.getState() != SyncState.CONFLICT)
089: throw new Exception(
090: "The specified entity is not in conflict");
091: if (resolution == SyncState.SYNC_EXT2DSY
092: || resolution == SyncState.DSY_OVERWRITE
093: || resolution == SyncState.CONFLICT_DSY_RULES) {
094: entity.setState(resolution);
095: if (resolution == SyncState.SYNC_EXT2DSY) {
096: // sync ext values to daisy
097: daisyEntityDao.storeEntity(entity);
098: }
099: // other resolutions need only change the sync status
100: } else {
101: throw new Exception("Cannot resolve conflict with "
102: + resolution);
103: }
104: syncEntityDao.storeEntity(entity);
105: }
106:
107: public void turnOffOverride(String documentId, long branchId,
108: long languageId, SyncState resolution) throws Exception {
109: VariantKey key = new VariantKey(documentId, branchId,
110: languageId);
111: Entity entity = syncEntityDao.getEntity(key);
112: if (entity.getState() != SyncState.CONFLICT_DSY_RULES)
113: throw new Exception(
114: "The specified entity is not in override mode");
115: if (resolution == SyncState.SYNC_EXT2DSY
116: || resolution == SyncState.DSY_OVERWRITE) {
117: entity.setState(resolution);
118: } else {
119: throw new Exception("Cannot turn off override with "
120: + resolution);
121: }
122: syncEntityDao.storeEntity(entity);
123: }
124:
125: public void recreateDeletedDocument(String documentId,
126: long branchId, long languageId) throws Exception {
127: VariantKey key = new VariantKey(documentId, branchId,
128: languageId);
129: Entity entity = syncEntityDao.getEntity(key);
130: if (!entity.isDaisyDeleted())
131: throw new Exception(
132: "The cannot recreate entity which has not been deleted");
133:
134: try {
135: Entity daisyEntity = daisyEntityDao.getEntity(key);
136: if (daisyEntity == null) {
137: entity.setDaisyVariantKey(null);
138: }
139: } catch (EntityNotFoundException e) {
140: entity.setDaisyVariantKey(null);
141: }
142:
143: entity.setDaisyDeleted(false);
144: entity.setState(SyncState.SYNC_EXT2DSY);
145: daisyEntityDao.storeEntity(entity);
146: syncEntityDao.replaceEntity(key, entity);
147: }
148:
149: public boolean startSynchronization() {
150: return this .synchronizer.startSync(true);
151: }
152:
153: public SystemState getLockState() {
154: return locker.getLockState();
155: }
156:
157: }
|