use of edu.stanford.muse.LabelManager.Label in project epadd by ePADD.
the class JSPHelper method createEditLabels.
/**
* handles create/edit label request received from edit-label.jsp.
* returns JSON with {status: (0 = passed, non-0 = failed), errorMessage: error message that can be shown to a user}
*/
public static String createEditLabels(HttpServletRequest request) {
JSONObject result = new JSONObject();
try {
Archive archive = JSPHelper.getArchive(request);
LabelManager.LabType labelType;
try {
labelType = LabelManager.LabType.valueOf(request.getParameter("labelType"));
} catch (Exception e) {
result.put("status", 1);
result.put("errorMessage", "Please specify a type of label.");
return result.toString();
}
String labelName = request.getParameter("labelName");
if (Util.nullOrEmpty(labelName)) {
result.put("status", 8);
result.put("errorMessage", "Label name cannot be empty.");
return result.toString();
}
String description = request.getParameter("labelDescription");
// check if it is label creation request or label updation one.
String labelID = request.getParameter("labelID");
LabelManager labelManager = archive.getLabelManager();
Label label = labelManager.getLabel(labelID);
// create the label if it doesn't exist, or update it if it does
if (label != null) {
label.update(labelName, description, labelType);
} else {
label = labelManager.createLabel(labelName, description, labelType);
}
// more processing for restriction labels
if (labelType == LabelManager.LabType.RESTRICTION) {
LabelManager.RestrictionType restrictionType = LabelManager.RestrictionType.valueOf(request.getParameter("restrictionType"));
// read extra fields for restricton labels only
long restrictedUntil = -1;
int restrictedForYears = -1;
// RESTRICTED_UNTIL processing
if (restrictionType == LabelManager.RestrictionType.RESTRICTED_UNTIL) {
// this is in the form yyyy-mm-dd
String restrictedUntilStr = request.getParameter("restrictedUntil");
try {
if (!Util.nullOrEmpty(restrictedUntilStr)) {
List<String> startTokens = Util.tokenize(restrictedUntilStr, "-");
int y = Integer.parseInt(startTokens.get(0));
// -1 because Calendar.MONTH starts from 0, while user will input from 1
int m = Integer.parseInt(startTokens.get(1)) - 1;
int d = Integer.parseInt(startTokens.get(2));
Calendar c = new GregorianCalendar();
c.set(Calendar.YEAR, y);
c.set(Calendar.MONTH, m);
c.set(Calendar.DATE, d);
restrictedUntil = c.getTime().getTime();
} else {
// empty string came in for restrictedUntil
result.put("status", 2);
result.put("errorMessage", "Please specify the date until which the restriction applies.");
return result.toString();
}
} catch (Exception e) {
result.put("status", 3);
result.put("errorMessage", "Invalid date string: " + Util.escapeHTML(restrictedUntilStr) + ". Please use the format yyyy-mm-dd. ");
return result.toString();
}
} else if (restrictionType == LabelManager.RestrictionType.RESTRICTED_FOR_YEARS) {
// RESTRICTED_FOR_YEARS processing
// this is in the form yyyy-mm-dd
String restrictedForYearsStr = request.getParameter("restrictedForYears");
try {
if (!Util.nullOrEmpty(restrictedForYearsStr)) {
restrictedForYears = Integer.parseInt(restrictedForYearsStr);
if (restrictedForYears <= 0) {
result.put("status", 4);
result.put("errorMessage", "Invalid value for #years: " + Util.escapeHTML(restrictedForYearsStr) + ". Number of years restricted should be positive.");
return result.toString();
}
} else {
// empty string came in for restrictedForYears
result.put("status", 5);
result.put("errorMessage", "Please specify the number of years for which the restriction applies.");
return result.toString();
}
} catch (Exception e) {
result.put("status", 6);
result.put("errorMessage", "Invalid value for #years: " + Util.escapeHTML(restrictedForYearsStr) + ". Please enter a number.");
return result.toString();
}
}
String labelAppliesToMessageText = request.getParameter("labelAppliesToMessageText");
label.setRestrictionDetails(restrictionType, restrictedUntil, restrictedForYears, labelAppliesToMessageText);
}
// if no exception happened, status is 0
result.put("status", 0);
result.put("labelID", label.getLabelID());
return result.toString();
} catch (Exception e) {
result.put("status", 7);
result.put("errorMessage", "Exception while saving label: " + e.getMessage());
return result.toString();
}
}
use of edu.stanford.muse.LabelManager.Label in project epadd by ePADD.
the class Archive method isTimeRestrictionExpired.
/*
Checks if the timerestrictionlabel applied to a given document has expired or not.
*/
private boolean isTimeRestrictionExpired(EmailDocument ed, Set<String> timedRestriction) {
// 1.means at least one timed restriction label on this doc. Check for the timed data
// if it is past current date/time then export else dont'
// 2.get those timed restrictions
boolean isTimedRestrictionExpired = true;
Date dt = ed.getDate();
Set<String> timedrestrictionsInDoc = Util.setIntersection(getLabelIDs(ed), timedRestriction);
// if any of the timedrestriction is not satisfied then don't export it.
for (String labid : timedrestrictionsInDoc) {
Label l = getLabelManager().getLabel(labid);
if (l.getRestrictionType() == LabelManager.RestrictionType.RESTRICTED_FOR_YEARS) {
int year = l.getRestrictedForYears();
Date future = new DateTime(dt).plusYears(year).toDate();
Date today = new DateTime().toDate();
if (// means the time is not over for this label
future.after(today))
// don't add doc
isTimedRestrictionExpired = false;
} else if (l.getRestrictionType() == LabelManager.RestrictionType.RESTRICTED_UNTIL) {
Date today = new DateTime().toDate();
if (// means the restriction time is not over
l.getRestrictedUntilTime() > today.getTime())
// don't add doc
isTimedRestrictionExpired = false;
}
}
return isTimedRestrictionExpired;
}
use of edu.stanford.muse.LabelManager.Label in project epadd by ePADD.
the class Archive method getLabelCountsAsJson.
/**
* returns an array (sorted by doc count for set labels.
* each element of the array is itself an array corresponding to the details for one label.
* important: labels with 0 count should not be returned.
*/
public JSONArray getLabelCountsAsJson(Collection<Document> docs) {
Map<String, Integer> labelIdToCount = new LinkedHashMap<>();
for (Label label : getLabelManager().getAllLabels()) {
labelIdToCount.put(label.getLabelID(), 0);
}
for (Document d : docs) {
Set<String> labelIds = labelManager.getLabelIDs(d.getUniqueId());
for (String labelId : labelIds) {
Integer I = labelIdToCount.getOrDefault(labelId, 0);
labelIdToCount.put(labelId, I + 1);
}
}
// sort by count
List<Pair<String, Integer>> pairs = Util.sortMapByValue(labelIdToCount);
// assemble the result json object
int count = 0;
JSONArray resultArray = new JSONArray();
for (Pair<String, Integer> p : pairs) {
String labelId = p.getFirst();
JSONArray array = new JSONArray();
Integer docCount = p.getSecond();
Label label = labelManager.getLabel(labelId);
array.put(0, labelId);
array.put(1, label.getLabelName());
array.put(2, label.getDescription());
array.put(3, docCount);
array.put(4, label.isSysLabel());
String labelTypeDescription = LabelManager.LabType.RESTRICTION.equals(label.getType()) ? "Restriction" : "General";
if (LabelManager.LabType.RESTRICTION.equals(label.getType()) && LabelManager.RestrictionType.RESTRICTED_FOR_YEARS.equals(label.getRestrictionType())) {
labelTypeDescription += " for " + Util.pluralize(label.getRestrictedForYears(), "year");
} else if (LabelManager.LabType.RESTRICTION.equals(label.getType()) && LabelManager.RestrictionType.RESTRICTED_UNTIL.equals(label.getRestrictionType())) {
long time = label.getRestrictedUntilTime();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
String description = sdf.format(new Date(time));
labelTypeDescription += " until " + description;
}
array.put(5, labelTypeDescription);
resultArray.put(count++, array);
}
return resultArray;
}
Aggregations