Refresh tag cell

This commit is contained in:
xseris 2017-12-08 00:46:59 +01:00
parent b16941777e
commit ee8ed8f563
5 changed files with 117 additions and 283 deletions

View File

@ -58,54 +58,48 @@ import com.google.refine.preference.PreferenceStore;
import com.google.refine.preference.TopList;
/**
* ProjectManager is responsible for loading and saving the workspace and
* projects.
* ProjectManager is responsible for loading and saving the workspace and projects.
*
*
*/
public abstract class ProjectManager {
// last n expressions used across all projects
static protected final int s_expressionHistoryMax = 100;
// If a project has been idle this long, flush it from memory
static protected final int PROJECT_FLUSH_DELAY = 1000 * 60 * 15; // 15
// minutes
static protected final int PROJECT_FLUSH_DELAY = 1000 * 60 * 15; // 15 minutes
// Don't spend more than this much time saving projects if doing a quick
// save
// Don't spend more than this much time saving projects if doing a quick save
static protected final int QUICK_SAVE_MAX_TIME = 1000 * 30; // 30 secs
protected Map<Long, ProjectMetadata> _projectsMetadata;
protected Map<String, Integer> _projectsTags;// TagName, number of projects
// having that tag
protected PreferenceStore _preferenceStore;
protected Map<String, Integer> _projectsTags;// TagName, number of projects having that tag
protected PreferenceStore _preferenceStore;
final static Logger logger = LoggerFactory.getLogger("ProjectManager");
/**
* What caches the joins between projects.
* What caches the joins between projects.
*/
transient protected InterProjectModel _interProjectModel = new InterProjectModel();
/**
* Flag for heavy operations like creating or importing projects. Workspace
* saves are skipped while it's set.
* Flag for heavy operations like creating or importing projects. Workspace saves are skipped while it's set.
*/
transient protected int _busy = 0;
/**
* While each project's metadata is loaded completely at start-up, each
* project's raw data is loaded only when the project is accessed by the
* user. This is because project metadata is tiny compared to raw project
* data. This hash map from project ID to project is more like a last
* accessed-last out cache.
* While each project's metadata is loaded completely at start-up, each project's raw data
* is loaded only when the project is accessed by the user. This is because project
* metadata is tiny compared to raw project data. This hash map from project ID to project
* is more like a last accessed-last out cache.
*/
transient protected Map<Long, Project> _projects;
static public ProjectManager singleton;
protected ProjectManager() {
protected ProjectManager(){
_projectsMetadata = new HashMap<Long, ProjectMetadata>();
_preferenceStore = new PreferenceStore();
_projects = new HashMap<Long, Project>();
@ -129,7 +123,6 @@ public abstract class ProjectManager {
/**
* Registers the project in the memory of the current session
*
* @param project
* @param projectMetadata
*/
@ -137,19 +130,8 @@ public abstract class ProjectManager {
synchronized (this) {
_projects.put(project.id, project);
_projectsMetadata.put(project.id, projectMetadata);
if (_projectsTags == null) {
if (_projectsTags == null)
_projectsTags = new HashMap<String, Integer>();
<<<<<<< Upstream, based on origin/master
}
String[] tags = projectMetadata.getTags();
if (tags != null) {
for (String tag : tags) {
if (_projectsTags.containsKey(tag)) {
_projectsTags.put(tag, _projectsTags.get(tag) + 1);
} else {
_projectsTags.put(tag, 1);
}
=======
String[] tags = projectMetadata.getTags();
if (tags != null) {
for (String tag : tags) {
@ -158,7 +140,6 @@ public abstract class ProjectManager {
} else {
_projectsTags.put(tag, 1);
}
>>>>>>> 529b6bd Modified tags in metadata dialog and moved column right to name
}
}
}
@ -166,7 +147,6 @@ public abstract class ProjectManager {
/**
* Load project metadata from data storage
*
* @param projectID
* @return
*/
@ -174,7 +154,6 @@ public abstract class ProjectManager {
/**
* Loads a project from the data store into memory
*
* @param id
* @return
*/
@ -182,32 +161,28 @@ public abstract class ProjectManager {
/**
* Import project from a Refine archive
*
* @param projectID
* @param inputStream
* @param gziped
* @throws IOException
*/
public abstract void importProject(long projectID, InputStream inputStream, boolean gziped)
throws IOException;
public abstract void importProject(long projectID, InputStream inputStream, boolean gziped) throws IOException;
/**
* Export project to a Refine archive
*
* @param projectId
* @param tos
* @throws IOException
*/
public abstract void exportProject(long projectId, TarOutputStream tos)
throws IOException;
public abstract void exportProject(long projectId, TarOutputStream tos) throws IOException;
/**
* Saves a project and its metadata to the data store
*
* @param id
*/
public void ensureProjectSaved(long id) {
synchronized (this) {
synchronized(this){
ProjectMetadata metadata = this.getProjectMetadata(id);
if (metadata != null) {
try {
@ -215,8 +190,7 @@ public abstract class ProjectManager {
} catch (Exception e) {
e.printStackTrace();
}
} // FIXME what should be the behaviour if metadata is null? i.e.
// not found
}//FIXME what should be the behaviour if metadata is null? i.e. not found
Project project = getProject(id);
if (project != null && metadata != null && metadata.getModified().after(project.getLastSave())) {
@ -225,36 +199,29 @@ public abstract class ProjectManager {
} catch (Exception e) {
e.printStackTrace();
}
} // FIXME what should be the behaviour if project is null? i.e. not
// found or loaded.
// FIXME what should happen if the metadata is found, but not the
// project? or vice versa?
}//FIXME what should be the behaviour if project is null? i.e. not found or loaded.
//FIXME what should happen if the metadata is found, but not the project? or vice versa?
}
}
/**
* Save project metadata to the data store
*
* @param metadata
* @param projectId
* @throws Exception
*/
public abstract void saveMetadata(ProjectMetadata metadata, long projectId)
throws Exception;
public abstract void saveMetadata(ProjectMetadata metadata, long projectId) throws Exception;
/**
* Save project to the data store
*
* @param project
* @throws IOException
*/
protected abstract void saveProject(Project project)
throws IOException;
protected abstract void saveProject(Project project) throws IOException;
/**
* Save workspace and all projects to data store
*
* @param allModified
*/
public void save(boolean allModified) {
@ -270,11 +237,10 @@ public abstract class ProjectManager {
protected abstract void saveWorkspace();
/**
* A utility class to prioritize projects for saving, depending on how long
* ago they have been changed but have not been saved.
* A utility class to prioritize projects for saving, depending on how long ago
* they have been changed but have not been saved.
*/
static protected class SaveRecord {
final Project project;
final long overdue;
@ -286,7 +252,6 @@ public abstract class ProjectManager {
/**
* Saves all projects to the data store
*
* @param allModified
*/
protected void saveProjects(boolean allModified) {
@ -296,16 +261,13 @@ public abstract class ProjectManager {
synchronized (this) {
for (long id : _projectsMetadata.keySet()) {
ProjectMetadata metadata = getProjectMetadata(id);
Project project = _projects.get(id); // don't call getProject()
// as that will load the
// project.
Project project = _projects.get(id); // don't call getProject() as that will load the project.
if (project != null) {
boolean hasUnsavedChanges = metadata.getModified().getTime() >= project.getLastSave().getTime();
// We use >= instead of just > to avoid the case where a
// newly created project
// has the same modified and last save times, resulting in
// the project not getting
boolean hasUnsavedChanges =
metadata.getModified().getTime() >= project.getLastSave().getTime();
// We use >= instead of just > to avoid the case where a newly created project
// has the same modified and last save times, resulting in the project not getting
// saved at all.
if (hasUnsavedChanges) {
@ -314,12 +276,11 @@ public abstract class ProjectManager {
records.add(new SaveRecord(project, msecsOverdue));
} else if (!project.getProcessManager().hasPending()
&& startTimeOfSave.getTime() - project.getLastSave().getTime() > PROJECT_FLUSH_DELAY) {
&& startTimeOfSave.getTime() - project.getLastSave().getTime() > PROJECT_FLUSH_DELAY) {
/*
* It's been a while since the project was last saved
* and it hasn't been modified. We can safely remove it
* from the cache to save some memory.
* It's been a while since the project was last saved and it hasn't been
* modified. We can safely remove it from the cache to save some memory.
*/
_projects.remove(id).dispose();
}
@ -329,7 +290,6 @@ public abstract class ProjectManager {
if (records.size() > 0) {
Collections.sort(records, new Comparator<SaveRecord>() {
@Override
public int compare(SaveRecord o1, SaveRecord o2) {
if (o1.overdue < o2.overdue) {
@ -342,17 +302,21 @@ public abstract class ProjectManager {
}
});
logger.info(allModified ? "Saving all modified projects ..." : "Saving some modified projects ...");
logger.info(allModified ?
"Saving all modified projects ..." :
"Saving some modified projects ..."
);
for (int i = 0; i < records.size()
&& (allModified || (new Date().getTime() - startTimeOfSave.getTime() < QUICK_SAVE_MAX_TIME)); i++) {
for (int i = 0;
i < records.size() &&
(allModified || (new Date().getTime() - startTimeOfSave.getTime() < QUICK_SAVE_MAX_TIME));
i++) {
try {
saveProject(records.get(i).project);
} catch (Exception e) {
e.printStackTrace();
// In case we're running low on memory, free as much as we
// can
// In case we're running low on memory, free as much as we can
disposeUnmodifiedProjects();
}
}
@ -369,7 +333,7 @@ public abstract class ProjectManager {
Project project = _projects.get(id);
if (project != null && !project.getProcessManager().hasPending()
&& metadata.getModified().getTime() < project.getLastSave().getTime()) {
_projects.remove(id).dispose();
_projects.remove(id).dispose();
}
}
}
@ -382,10 +346,10 @@ public abstract class ProjectManager {
return _interProjectModel;
}
/**
* Gets the project metadata from memory Requires that the metadata has
* already been loaded from the data store
*
* Gets the project metadata from memory
* Requires that the metadata has already been loaded from the data store
* @param id
* @return
*/
@ -394,9 +358,8 @@ public abstract class ProjectManager {
}
/**
* Gets the project metadata from memory Requires that the metadata has
* already been loaded from the data store
*
* Gets the project metadata from memory
* Requires that the metadata has already been loaded from the data store
* @param name
* @return
*/
@ -410,12 +373,12 @@ public abstract class ProjectManager {
}
/**
* Tries to find the project id when given a project name Requires that all
* project metadata exists has been loaded to memory from the data store
*
* Tries to find the project id when given a project name
* Requires that all project metadata exists has been loaded to memory from the data store
* @param name
* The name of the project
* @return The id of the project, or -1 if it cannot be found
* The name of the project
* @return
* The id of the project, or -1 if it cannot be found
*/
public long getProjectID(String name) {
for (Entry<Long, ProjectMetadata> entry : _projectsMetadata.entrySet()) {
@ -428,18 +391,18 @@ public abstract class ProjectManager {
/**
* 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"));
return (placeHolderJsonObj != null &&
placeHolderJsonObj.has("name") &&
placeHolderJsonObj.has("display"));
}
public void mergeEmptyUserMetadata(ProjectMetadata metadata) {
if (metadata == null) {
if (metadata == null)
return;
}
// place holder
JSONArray userMetadataPreference = null;
@ -449,14 +412,12 @@ public abstract class ProjectManager {
initDisplay(jsonObjArray);
try {
String userMeta = (String) _preferenceStore.get(PreferenceStore.USER_METADATA_KEY);
if (userMeta == null) {
String userMeta = (String)_preferenceStore.get(PreferenceStore.USER_METADATA_KEY);
if (userMeta == null)
return;
}
userMetadataPreference = new JSONArray(userMeta);
} catch (JSONException e1) {
logger.warn(
"wrong definition of userMetadata format. Please use form [{\"name\": \"client name\", \"display\":true}, {\"name\": \"progress\", \"display\":false}]");
logger.warn("wrong definition of userMetadata format. Please use form [{\"name\": \"client name\", \"display\":true}, {\"name\": \"progress\", \"display\":false}]");
logger.error(ExceptionUtils.getFullStackTrace(e1));
}
@ -482,18 +443,18 @@ public abstract class ProjectManager {
if (!found) {
placeHolderJsonObj.put("value", "");
metadata.getUserMetadata().put(placeHolderJsonObj);
logger.info("Put the placeholder {} for project {}", placeHolderJsonObj.getString("name"),
logger.info("Put the placeholder {} for project {}",
placeHolderJsonObj.getString("name"),
metadata.getName());
}
} catch (JSONException e) {
logger.warn("Exception when mergeEmptyUserMetadata", e);
logger.warn("Exception when mergeEmptyUserMetadata",e);
}
}
}
/**
* honor the meta data preference
*
* @param jsonObjArray
*/
private void initDisplay(JSONArray jsonObjArray) {
@ -509,11 +470,10 @@ public abstract class ProjectManager {
/**
* Gets all the project Metadata currently held in memory.
*
* @return
*/
public Map<Long, ProjectMetadata> getAllProjectMetadata() {
for (Project project : _projects.values()) {
for(Project project : _projects.values()) {
mergeEmptyUserMetadata(project.getMetadata());
}
@ -526,16 +486,16 @@ public abstract class ProjectManager {
* @return
*/
public Map<String, Integer> getAllProjectTags() {
return _projectsTags;
return _projectsTags;
}
/**
* Gets the required project from the data store If project does not already
* exist in memory, it is loaded from the data store
*
* Gets the required project from the data store
* If project does not already exist in memory, it is loaded from the data store
* @param id
* the id of the project
* @return the project with the matching id, or null if it can't be found
* the id of the project
* @return
* the project with the matching id, or null if it can't be found
*/
public Project getProject(long id) {
synchronized (this) {
@ -553,7 +513,6 @@ public abstract class ProjectManager {
/**
* Gets the preference store
*
* @return
*/
public PreferenceStore getPreferenceStore() {
@ -562,7 +521,6 @@ public abstract class ProjectManager {
/**
* Gets all expressions from the preference store
*
* @return
*/
public List<String> getExpressions() {
@ -571,14 +529,13 @@ public abstract class ProjectManager {
/**
* The history entry manager deals with changes
*
* @return manager for handling history
*/
public abstract HistoryEntryManager getHistoryEntryManager();
/**
* Remove the project from the data store
*
* @param project
*/
public void deleteProject(Project project) {
@ -587,17 +544,15 @@ public abstract class ProjectManager {
/**
* Remove project from data store
*
* @param projectID
*/
public abstract void deleteProject(long projectID);
/**
* Removes project from memory
*
* @param projectID
*/
protected void removeProject(long projectID) {
protected void removeProject(long projectID){
if (_projects.containsKey(projectID)) {
_projects.remove(projectID).dispose();
}
@ -606,10 +561,10 @@ public abstract class ProjectManager {
}
}
/**
* Sets the flag for long running operations. This will prevent workspace
* saves from happening while it's set.
*
* Sets the flag for long running operations. This will prevent
* workspace saves from happening while it's set.
* @param busy
*/
public void setBusy(boolean busy) {
@ -622,9 +577,10 @@ public abstract class ProjectManager {
}
}
/**
* Add the latest expression to the preference store
*
* @param s
*/
public void addLatestExpression(String s) {
@ -633,12 +589,13 @@ public abstract class ProjectManager {
}
}
/**
*
* @param ps
*/
static protected void preparePreferenceStore(PreferenceStore ps) {
ps.put("scripting.expressions", new TopList(s_expressionHistoryMax));
ps.put("scripting.starred-expressions", new TopList(Integer.MAX_VALUE));
}
*
* @param ps
*/
static protected void preparePreferenceStore(PreferenceStore ps) {
ps.put("scripting.expressions", new TopList(s_expressionHistoryMax));
ps.put("scripting.starred-expressions", new TopList(Integer.MAX_VALUE));
}
}

View File

@ -94,15 +94,9 @@ public class ProjectManagerTests extends RefineTest {
SUT.registerProject(project, metadata);
<<<<<<< Upstream, based on origin/master
AssertProjectRegistered();
verify(metadata, times(1)).getTags();
=======
AssertProjectRegistered();
verify(metadata, times(1)).getTags();
>>>>>>> 529b6bd Modified tags in metadata dialog and moved column right to name
verifyNoMoreInteractions(project);
verifyNoMoreInteractions(metadata);
}
@ -127,11 +121,7 @@ public class ProjectManagerTests extends RefineTest {
this.verifySaveTimeCompared(1);
verify(SUT, times(1)).saveProject(project);
verify(metadata, times(1)).getTags();
<<<<<<< Upstream, based on origin/master
=======
>>>>>>> 529b6bd Modified tags in metadata dialog and moved column right to name
//ensure end
verifyNoMoreInteractions(project);
verifyNoMoreInteractions(metadata);

View File

@ -351,18 +351,12 @@ Refine.TagsManager._getAllProjectTags = function() {
});
array.map(function(item){
<<<<<<< Upstream, based on origin/master
self.allProjectTags.push(item);
=======
self.allProjectTags.push(item);
});
},
async : false
>>>>>>> 529b6bd Modified tags in metadata dialog and moved column right to name
});
},
async : false
});
}
}
return self.allProjectTags;
};

View File

@ -23,10 +23,10 @@ function EditMetadataDialog(metaData, targetRowElem) {
if(key==="tags"){
$('<button class="button">').text($.i18n._('core-index')["edit"]).appendTo(td2).click(function() {
var oldTags = $(td1).text().replace("\"","").replace("[","").replace("]","");
var oldTags = $(td1).text().replace("[","").replace("]","");
oldTags = replaceAll(oldTags,"\"","");
var newTags = window.prompt($.i18n._('core-index')["change-metadata-value"]+" " + key, value);
newTags = newTags.replace("\"","").replace("[","").replace("]","");
var newTags = window.prompt($.i18n._('core-index')["change-metadata-value"]+" " + key, $(td1).text());
newTags = newTags.replace("[","").replace("]","");
newTags = replaceAll(newTags,"\"","");
if (newTags !== null) {
$(td1).text(newTags);
@ -43,7 +43,7 @@ function EditMetadataDialog(metaData, targetRowElem) {
});
}
Refine.OpenProjectUI.refreshProject(targetRowElem, metaData);
Refine.OpenProjectUI.refreshProject(targetRowElem, metaData, project);
});
}
@ -74,7 +74,7 @@ function EditMetadataDialog(metaData, targetRowElem) {
);
}
Refine.OpenProjectUI.refreshProject(targetRowElem, metaData);
Refine.OpenProjectUI.refreshProject(targetRowElem, metaData, project);
});
}
};

View File

@ -275,41 +275,6 @@ Refine.OpenProjectUI.prototype._renderProjects = function(data) {
});
var editTagsLink = $('<a></a>')
.text($.i18n._('core-index-open')["rename"])
.addClass("secondary")
.addClass("edit-project-tags")
.css("visibility", "hidden")
.attr("href", "")
.attr("title", $.i18n._('core-index-open')["edit-tags"])
.html('<img src="images/edit.png" />')
.click(function() {
var oldTags = project.tags.join(",");
var newTags = window.prompt($.i18n._('core-index-open')["edit-tags-desc"],oldTags);
if (newTags === null || newTags === oldTags) {
return false;
}
$.ajax({
type : "POST",
url : "command/core/set-project-tags",
data : {
"project" : project.id,
"old" : oldTags,
"new" : newTags
},
dataType : "json",
success : function(data) {
if (data && typeof data.code != 'undefined' && data.code == "ok") {
Refine.TagsManager.allProjectTags = [];
self._buildTagsAndFetchProjects();
} else {
alert($.i18n._('core-index-open')["warning-tags-update"]+ " "+ data.message);
}
}
});
return false;
}).appendTo(tagsCell);
var appendMetaField = function(data) {
$('<div></div>')
.html(data)
@ -329,11 +294,6 @@ Refine.OpenProjectUI.prototype._renderProjects = function(data) {
}
}
$(tr).mouseenter(function() {
editTagsLink.css("visibility", "visible");
}).mouseleave(function() {
editTagsLink.css("visibility", "hidden");
});
};
@ -388,7 +348,7 @@ Refine.OpenProjectUI.prototype._onClickUploadFileButton = function(evt) {
return false;
};
Refine.OpenProjectUI.refreshProject = function(tr, metaData) {
Refine.OpenProjectUI.refreshProject = function(tr, metaData, project) {
var refreshMetaField = function(data, index) {
if (index === 3) {
@ -409,43 +369,9 @@ Refine.OpenProjectUI.refreshProject = function(tr, metaData) {
.text(tag)
.attr("title", $.i18n._('core-index-open')["edit-tags"])
.appendTo(tagCol);
var editTagsLink = $('<a></a>')
.text($.i18n._('core-index-open')["rename"])
.addClass("secondary")
.addClass("edit-project-tags")
.css("visibility", "hidden")
.attr("href", "")
.attr("title", $.i18n._('core-index-open')["edit-tags"])
.html('<img src="images/edit.png" />')
.click(function() {
var oldTags = project.tags.join(",");
var newTags = window.prompt($.i18n._('core-index-open')["edit-tags-desc"],oldTags);
if (newTags === null || newTags === oldTags) {
return false;
}
$.ajax({
type : "POST",
url : "command/core/set-project-tags",
data : {
"project" : project.id,
"old" : oldTags,
"new" : newTags
},
dataType : "json",
success : function(data) {
if (data && typeof data.code != 'undefined' && data.code == "ok") {
Refine.TagsManager.allProjectTags = [];
self._buildTagsAndFetchProjects();
} else {
alert($.i18n._('core-index-open')["warning-tags-update"]+ " "+ data.message);
}
}
});
return false;
}).appendTo(tagCol);
tagCol.parent().addClass(tag);
tagCol.parent().addClass(tag);
});
} else{
data.split(",").map(function(tag){
var tagsCell = $("<span/>")
@ -453,44 +379,11 @@ Refine.OpenProjectUI.refreshProject = function(tr, metaData) {
.text(tag)
.attr("title", $.i18n._('core-index-open')["edit-tags"])
.appendTo(tagCol);
var editTagsLink = $('<a></a>')
.text($.i18n._('core-index-open')["rename"])
.addClass("secondary")
.addClass("edit-project-tags")
.css("visibility", "hidden")
.attr("href", "")
.attr("title", $.i18n._('core-index-open')["edit-tags"])
.html('<img src="images/edit.png" />')
.click(function() {
var oldTags = project.tags.join(",");
var newTags = window.prompt($.i18n._('core-index-open')["edit-tags-desc"],oldTags);
if (newTags === null || newTags === oldTags) {
return false;
}
$.ajax({
type : "POST",
url : "command/core/set-project-tags",
data : {
"project" : project.id,
"old" : oldTags,
"new" : newTags
},
dataType : "json",
success : function(data) {
if (data && typeof data.code != 'undefined' && data.code == "ok") {
Refine.TagsManager.allProjectTags = [];
self._buildTagsAndFetchProjects();
} else {
alert($.i18n._('core-index-open')["warning-tags-update"]+ " "+ data.message);
}
}
});
return false;
}).appendTo(tagCol);
tagCol.parent().addClass(tag);
});
}
};
var index = 3;