edit the user metadata

This commit is contained in:
Jacky 2017-10-29 13:46:37 -04:00
parent 384e69aa5f
commit 1777a6ecf3
6 changed files with 200 additions and 25 deletions

View File

@ -41,6 +41,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -72,10 +73,13 @@ public class ProjectMetadata implements Jsonizable {
// import options is an array for 1-n data sources
private JSONArray _importOptionMetaData = new JSONArray();
// user metadata
private JSONArray _userMetadata = new JSONArray();;
private Map<String, Serializable> _customMetadata = new HashMap<String, Serializable>();
private PreferenceStore _preferenceStore = new PreferenceStore();
final Logger logger = LoggerFactory.getLogger("project_metadata");
final static Logger logger = LoggerFactory.getLogger("project_metadata");
protected ProjectMetadata(Date date) {
_created = date;
@ -121,6 +125,12 @@ public class ProjectMetadata implements Jsonizable {
writer.value(_importOptionMetaData);
}
// write user metadata in {name, value, display} form:
if (_userMetadata.length() > 0) {
writer.key(PreferenceStore.USER_METADATA_KEY);
writer.value(_userMetadata);
}
if (isSaveMode(options)) {
writer.key("password"); writer.value(_password);
@ -190,7 +200,7 @@ public class ProjectMetadata implements Jsonizable {
try {
pm._preferenceStore.load(obj.getJSONObject("preferences"));
} catch (JSONException e) {
// ignore
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
@ -199,7 +209,7 @@ public class ProjectMetadata implements Jsonizable {
((TopList) pm._preferenceStore.get("scripting.expressions"))
.load(obj.getJSONArray("expressions"));
} catch (JSONException e) {
// ignore
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
@ -217,7 +227,7 @@ public class ProjectMetadata implements Jsonizable {
}
}
} catch (JSONException e) {
// ignore
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
@ -226,10 +236,18 @@ public class ProjectMetadata implements Jsonizable {
JSONArray jsonArray = obj.getJSONArray("importOptionMetaData");
pm._importOptionMetaData = jsonArray;
} catch (JSONException e) {
// ignore
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
if (obj.has(PreferenceStore.USER_METADATA_KEY) && !obj.isNull(PreferenceStore.USER_METADATA_KEY)) {
try {
JSONArray jsonArray = obj.getJSONArray(PreferenceStore.USER_METADATA_KEY);
pm._userMetadata = jsonArray;
} catch (JSONException e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
pm.written = new Date(); // Mark it as not needing writing until modified
@ -381,15 +399,39 @@ public class ProjectMetadata implements Jsonizable {
updateModified();
}
public JSONArray getUserMetadata() {
return _userMetadata;
}
public void setUserMetadata(JSONArray userMetadata) {
this._userMetadata = userMetadata;
}
private void updateUserMetadata(String metaName, String valueString) {
for (int i = 0; i < _userMetadata.length(); i++) {
try {
JSONObject obj = _userMetadata.getJSONObject(i);
if (obj.getString("name").equals(metaName)) {
obj.put("value", valueString);
}
} catch (JSONException e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}
}
public void setAnyField(String metaName, String valueString) {
Class<? extends ProjectMetadata> metaClass = this.getClass();
try {
Field metaField = metaClass.getDeclaredField("_" + metaName);
metaField.set(this, valueString);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
// do nothing
e.printStackTrace();
} catch (NoSuchFieldException e) {
updateUserMetadata(metaName, valueString);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
logger.error(ExceptionUtils.getFullStackTrace(e));
}
}

View File

@ -44,6 +44,8 @@ import java.io.OutputStream;
import java.util.Properties;
import java.util.zip.GZIPInputStream;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.apache.tools.tar.TarOutputStream;
@ -59,8 +61,10 @@ import com.google.refine.ProjectManager;
import com.google.refine.ProjectMetadata;
import com.google.refine.history.HistoryEntryManager;
import com.google.refine.model.Project;
import com.google.refine.preference.PreferenceStore;
import com.google.refine.preference.TopList;
public class FileProjectManager extends ProjectManager {
final static protected String PROJECT_DIR_SUFFIX = ".project";
@ -356,6 +360,11 @@ public class FileProjectManager extends ProjectManager {
JSONTokener tokener = new JSONTokener(reader);
JSONObject obj = (JSONObject) tokener.nextValue();
// load global preferences firstly
if (obj.has("preferences") && !obj.isNull("preferences")) {
_preferenceStore.load(obj.getJSONObject("preferences"));
}
JSONArray a = obj.getJSONArray("projectIDs");
int count = a.length();
for (int i = 0; i < count; i++) {
@ -364,11 +373,9 @@ public class FileProjectManager extends ProjectManager {
File projectDir = getProjectDir(id);
ProjectMetadata metadata = ProjectMetadataUtilities.load(projectDir);
_projectsMetadata.put(id, metadata);
}
mergeEmptyUserMetadata(metadata);
if (obj.has("preferences") && !obj.isNull("preferences")) {
_preferenceStore.load(obj.getJSONObject("preferences"));
_projectsMetadata.put(id, metadata);
}
if (obj.has("expressions") && !obj.isNull("expressions")) { // backward compatibility
@ -395,6 +402,53 @@ public class FileProjectManager extends ProjectManager {
return found;
}
private void mergeEmptyUserMetadata(ProjectMetadata metadata) {
if (metadata == null)
return;
// place holder
JSONArray userMetadataPreference = null;
// actual metadata for project
JSONArray jsonObjArray = metadata.getUserMetadata();
try {
userMetadataPreference = new JSONArray((String)_preferenceStore.get(PreferenceStore.USER_METADATA_KEY));
} catch (JSONException e1) {
logger.error(ExceptionUtils.getFullStackTrace(e1));
}
if (userMetadataPreference == null) {
logger.warn("wrong definition of userMetadata format. Please use form [{\"name\": \"client name\", \"display\":true}, {\"name\": \"progress\", \"display\":false}]");
return;
}
for (int index = 0; index < userMetadataPreference.length(); index++) {
try {
boolean found = false;
JSONObject placeHolderJsonObj = userMetadataPreference.getJSONObject(index);
for (int i = 0; i < jsonObjArray.length(); i++) {
JSONObject jsonObj = jsonObjArray.getJSONObject(i);
if (jsonObj.getString("name").equals(placeHolderJsonObj.getString("name"))) {
found = true;
jsonObj.put("display", placeHolderJsonObj.get("display"));
break;
}
}
if (!found) {
placeHolderJsonObj.put("value", "");
metadata.getUserMetadata().put(placeHolderJsonObj);
logger.info("Put the placeholder {} for project {}",
placeHolderJsonObj.getString("name"),
metadata.getName());
}
} catch (JSONException e) {
logger.warn("Exception when mergeEmptyUserMetadata",e);
}
}
}
protected void recover() {
boolean recovered = false;
for (File file : _workspaceDir.listFiles()) {

View File

@ -48,6 +48,8 @@ import com.google.refine.Jsonizable;
import com.google.refine.RefineServlet;
public class PreferenceStore implements Jsonizable {
public static final String USER_METADATA_KEY = "userMetaData";
private boolean dirty = false;
protected Map<String, Object> _prefs = new HashMap<String, Object>();

View File

@ -1,12 +1,11 @@
function EditMetadataDialog(metaData) {
function EditMetadataDialog(metaData, targetRowElem) {
this._metaDataUIs = [];
this._metaData = metaData;
this._MetaDataUI = function(tr, key, value, project) {
var self = this;
var td0 = tr.insertCell(0);
$(td0).text(key);
@ -35,6 +34,8 @@ function EditMetadataDialog(metaData) {
"json"
);
}
Refine.OpenProjectUI.refreshProject(targetRowElem, metaData);
});
}
@ -61,16 +62,32 @@ EditMetadataDialog.prototype._createDialog = function() {
.html('<tr><th>'+$.i18n._('core-index')["key"]+'</th><th>'+$.i18n._('core-index')["value"]+'</th><th></th></tr>')
.appendTo(body)[0];
for (var k in this._metaData) {
var flattenObject = function(ob, key) {
var toReturn = {};
for ( var i in ob) {
if (i !== key) {
toReturn[i] = ob[i];
continue;
}
for ( var x in ob[i]) {
toReturn[ob[i][x].name] = ob[i][x].value;
}
}
return toReturn;
};
var flatMetaData = flattenObject(this._metaData, "userMetaData");
for (var k in flatMetaData) {
var tr = metadataTable.insertRow(metadataTable.rows.length);
if (typeof this._metaData[k] === 'string')
v = this._metaData[k].replace(/\"/g, "");
if (typeof flatMetaData[k] === 'string')
v = flatMetaData[k].replace(/\"/g, "");
else
v = JSON.stringify(this._metaData[k]);
v = JSON.stringify(flatMetaData[k]);
this._metaDataUIs.push(new this._MetaDataUI(tr, k, v, this._metaData.id));
this._metaDataUIs.push(new this._MetaDataUI(tr, k, v, flatMetaData.id));
}
}

View File

@ -211,7 +211,7 @@ Refine.OpenProjectUI.prototype._renderProjects = function(data) {
.attr("href", "javascript:{}")
.css("visibility", "hidden")
.click(function() {
new EditMetadataDialog(project);
new EditMetadataDialog(project, $(this).parent().parent());
})
.appendTo(
$(tr.insertCell(tr.cells.length)).css('width', '6%')
@ -229,6 +229,29 @@ Refine.OpenProjectUI.prototype._renderProjects = function(data) {
.attr("href", "project?project=" + project.id)
.appendTo(tr.insertCell(tr.cells.length));
var appendMetaField = function(data, width) {
var width = width || '1%';
$('<div></div>')
.html(data)
.appendTo($(tr.insertCell(tr.cells.length)).attr('width', width));
};
appendMetaField(project.creator);
appendMetaField(project.subject);
appendMetaField(project.description, '20%');
appendMetaField(project.rowNumber);
var data = project.userMetaData;
for(var i in data)
{
if (data[i].display === true) {
appendMetaField(data[i].value);
}
}
console.log('===================');
console.log(JSON.stringify(project));
// END TODO
$(tr).mouseenter(function() {
renameLink.css("visibility", "visible");
deleteLink.css("visibility", "visible");
@ -276,6 +299,43 @@ Refine.OpenProjectUI.prototype._onClickUploadFileButton = function(evt) {
return false;
};
Refine.OpenProjectUI.refreshProject = function(tr, metaData) {
var refreshMetaField = function(data, index) {
$('td', tr).eq(index)
.html(data);
};
var index = 5;
refreshMetaField(metaData.creator, index); index++;
refreshMetaField(metaData.subject,index); index++;
refreshMetaField(metaData.description,index); index++;
refreshMetaField(metaData.rowNumbe,index); index++;
var updateUserMetaData = function(ob) {
var userMetaData = ob.userMetaData;
for ( var n in ob) {
for ( var i in userMetaData) {
if (n === userMetaData[i].name) {
userMetaData[i].value = ob[n];
break;
}
}
}
ob.userMetaData = userMetaData;
};
updateUserMetaData(metaData);
var data = metaData.userMetaData;
for(var i in data)
{
if (data[i].display === true) {
refreshMetaField(data[i].value,index); index++;
}
}
}
Refine.actionAreas.push({
id: "open-project",
label: $.i18n._('core-index-open')["open-proj"],