public <T extends ISocioObject> void saveObject(T object) throws DuplicateMySocioObjectException { if (object instanceof IUniqueObject) { IUniqueObject uniqueObject = (IUniqueObject) object; Cache cache = cm.getCache("Objects"); String key = uniqueObject.getUniqueFieldName() + uniqueObject.getUniqueFieldValue(); Element element = cache.get(key); if (element != null) { ((SocioObject) object).setId((ObjectId) element.getValue()); return; } @SuppressWarnings("unchecked") Query<T> q = (Query<T>) ds.createQuery(object.getClass()) .field(uniqueObject.getUniqueFieldName()) .equal(uniqueObject.getUniqueFieldValue()); T objectT = (T) q.get(); if (objectT != null) { ((SocioObject) object).setId(objectT.getId()); element = new Element(key, objectT.getId()); cache.put(element); logger.info( "Duplicate object of type: " + object.getClass() + " for query: " + q.toString()); throw new DuplicateMySocioObjectException( "Duplicate object of type: " + object.getClass() + " for query: " + q.toString()); } } ds.save(object); }
@Override public <T extends AbstractUserMessagesProcessor> void saveProcessor( T processor, String uniqueFieldName, String uniqueFieldValue) throws DuplicateMySocioObjectException { @SuppressWarnings("unchecked") Query<T> q = (Query<T>) processorsDs .createQuery(processor.getClass()) .field(uniqueFieldName) .equal(uniqueFieldValue); String userId = processor.getUserId(); if (userId != null) { q.field("userId").equal(userId); } AbstractUserMessagesProcessor existingProcessor = q.get(); if (existingProcessor != null) { processor.setId(existingProcessor.getId()); logger.info("Duplicate processor for query: " + q.toString()); throw new DuplicateMySocioObjectException("Duplicate processor for query: " + q.toString()); } processorsDs.save(processor); }