From 8187799f85d301262acb93abd00c9ce6ab5329a9 Mon Sep 17 00:00:00 2001 From: Jacky Date: Fri, 17 Nov 2017 14:38:02 -0500 Subject: [PATCH] fix the row count refresh issue --- .../src/com/google/refine/ProjectManager.java | 85 ++++++++++++++++++- .../refine/commands/row/GetRowsCommand.java | 3 + .../google/refine/io/FileProjectManager.java | 68 --------------- 3 files changed, 87 insertions(+), 69 deletions(-) diff --git a/main/src/com/google/refine/ProjectManager.java b/main/src/com/google/refine/ProjectManager.java index 48de19144..b69b0945b 100644 --- a/main/src/com/google/refine/ProjectManager.java +++ b/main/src/com/google/refine/ProjectManager.java @@ -44,7 +44,11 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.tools.tar.TarOutputStream; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -370,13 +374,92 @@ public abstract class ProjectManager { } return -1; } + + /** + * A valid user meta data definition should have name and display property + * @param placeHolderJsonObj + * @return + */ + private boolean isValidUserMetadataDefinition(JSONObject placeHolderJsonObj) { + return (placeHolderJsonObj != null && + placeHolderJsonObj.has("name") && + placeHolderJsonObj.has("display")); + } + + public void mergeEmptyUserMetadata(ProjectMetadata metadata) { + if (metadata == null) + return; + + // place holder + JSONArray userMetadataPreference = null; + // actual metadata for project + JSONArray jsonObjArray = metadata.getUserMetadata(); + + // honor the meta data preference + for (int index = 0; index < jsonObjArray.length(); index++) { + try { + JSONObject projectMetaJsonObj = jsonObjArray.getJSONObject(index); + projectMetaJsonObj.put("display", false); + } catch (JSONException e) { + logger.error(ExceptionUtils.getFullStackTrace(e)); + } + } + + try { + String userMeta = (String)_preferenceStore.get(PreferenceStore.USER_METADATA_KEY); + if (userMeta == null) + return; + userMetadataPreference = new JSONArray(userMeta); + } 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); + + if (!isValidUserMetadataDefinition(placeHolderJsonObj)) { + logger.warn("Skipped invalid user metadata definition" + placeHolderJsonObj.toString()); + continue; + } + 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); + } + } + } /** - * Gets all the project Metadata currently held in memory + * Gets all the project Metadata currently held in memory. * @return */ public Map getAllProjectMetadata() { + for(Project project : _projects.values()) { + mergeEmptyUserMetadata(project.getMetadata()); + } + return _projectsMetadata; } diff --git a/main/src/com/google/refine/commands/row/GetRowsCommand.java b/main/src/com/google/refine/commands/row/GetRowsCommand.java index 887fe9222..8feb8c5f4 100644 --- a/main/src/com/google/refine/commands/row/GetRowsCommand.java +++ b/main/src/com/google/refine/commands/row/GetRowsCommand.java @@ -178,6 +178,9 @@ public class GetRowsCommand extends Command { if (callback != null) { writer.write(")"); } + + // metadata refresh for row mode and record mode + project.getMetadata().setRowCount(project.rows.size()); } catch (Exception e) { respondException(response, e); } diff --git a/main/src/com/google/refine/io/FileProjectManager.java b/main/src/com/google/refine/io/FileProjectManager.java index f0a5211d7..6d9ac6ce3 100644 --- a/main/src/com/google/refine/io/FileProjectManager.java +++ b/main/src/com/google/refine/io/FileProjectManager.java @@ -44,7 +44,6 @@ import java.io.OutputStream; import java.util.Properties; import java.util.zip.GZIPInputStream; -import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.tools.tar.TarEntry; import org.apache.tools.tar.TarInputStream; import org.apache.tools.tar.TarOutputStream; @@ -60,7 +59,6 @@ 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; @@ -403,72 +401,6 @@ public class FileProjectManager extends ProjectManager { return found; } - public void mergeEmptyUserMetadata(ProjectMetadata metadata) { - if (metadata == null) - return; - - // place holder - JSONArray userMetadataPreference = null; - // actual metadata for project - JSONArray jsonObjArray = metadata.getUserMetadata(); - - try { - String userMeta = (String)_preferenceStore.get(PreferenceStore.USER_METADATA_KEY); - if (userMeta == null) - return; - userMetadataPreference = new JSONArray(userMeta); - } 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); - - if (!isValidUserMetadataDefinition(placeHolderJsonObj)) { - logger.warn("Skipped invalid user metadata definition" + placeHolderJsonObj.toString()); - continue; - } - - 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); - } - } - } - - /** - * A valid user meta data definition should have name and display property - * @param placeHolderJsonObj - * @return - */ - private boolean isValidUserMetadataDefinition(JSONObject placeHolderJsonObj) { - return (placeHolderJsonObj != null && - placeHolderJsonObj.has("name") && - placeHolderJsonObj.has("display")); - } - protected void recover() { boolean recovered = false; for (File file : _workspaceDir.listFiles()) {