Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.findwise.hydra.local;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import com.findwise.hydra.Document;
import com.findwise.hydra.DocumentID;
import com.findwise.hydra.JsonException;
import com.findwise.hydra.SerializationUtils;
import com.findwise.tools.Comparator;
import com.google.gson.JsonParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LocalDocument implements Document {
private static Logger internalLogger = LoggerFactory.getLogger("internal");
private Map documentMap;
private Set touchedContent;
private Set touchedMetadata;
private boolean touchedAction;
public LocalDocument() {
documentMap = new HashMap();
documentMap.put(CONTENTS_KEY, new HashMap());
documentMap.put(METADATA_KEY, new HashMap());
touchedContent = new HashSet();
touchedMetadata = new HashSet();
touchedAction = false;
}
public LocalDocument(String json) throws JsonException {
this();
fromJson(json);
// The touched-sets will have been updated by fromJson,
// let's clean that up in this case
markSynced();
}
@Override
public Action getAction() {
return (Action) documentMap.get(ACTION_KEY);
}
@Override
public void setAction(Action action) {
documentMap.put(ACTION_KEY, action);
touchedAction = true;
}
/**
* Marks all outstanding changes as in sync with the database.
*/
public final void markSynced() {
touchedContent.clear();
touchedMetadata.clear();
touchedAction = false;
}
/**
* @return true if there are no outstanding changes
*/
public boolean isSynced() {
return (touchedContent.size() + touchedMetadata.size()) == 0 && !touchedAction;
}
@Override
public boolean hasContentField(String fieldName) {
return getContentMap().containsKey(fieldName) && getContentMap().get(fieldName) != null;
}
@SuppressWarnings("unchecked")
@Override
public boolean hasMetadataField(String fieldName) {
return ((Map)documentMap.get(METADATA_KEY)).containsKey(fieldName);
}
@Override
public LocalDocumentID getID() {
if(!documentMap.containsKey(ID_KEY)) {
return null;
}
return new LocalDocumentID(documentMap.get(ID_KEY));
}
public void setID(DocumentID id) {
documentMap.put(ID_KEY, id.getID());
}
/**
* Returns the backing map of this document. Beware that any changes
* to this structure directly, will not be saved properly! If you wish
* to modify, use removeContentField() and putContentField() instead.
*/
@SuppressWarnings("unchecked")
public Map getContentMap() {
return ((Map)documentMap.get(CONTENTS_KEY));
}
@SuppressWarnings("unchecked")
public Map getMetadataMap() {
return ((Map)documentMap.get(METADATA_KEY));
}
@Override
public final Object putContentField(String fieldName, Object value) {
fieldName = removePeriodFromKey(fieldName);
touchedContent.add(fieldName);
return getContentMap().put(fieldName, value);
}
private Object putMetadataField(String fieldName, Object value) {
fieldName = removePeriodFromKey(fieldName);
touchedMetadata.add(fieldName);
return getMetadataMap().put(fieldName, value);
}
/**
* Appends a value to a content field, converting the field into a list if it is not already one.
* WARNING: This method does not check the type of inserted values, or the type of the field it appends to
*
* @param fieldName content field
* @param value the value to append with
*/
@SuppressWarnings("unchecked")
public void appendToContentField(String fieldName, Object value) {
List