Skip to content

Commit

Permalink
LSNBLDR-517; prepare for multiple columns
Browse files Browse the repository at this point in the history
  • Loading branch information
clhedrick committed Aug 11, 2015
1 parent 6fc3397 commit 069a96d
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 529 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface SimplePageItem {
public static final int QUESTION = 11;
public static final int BLTI = 12;
public static final int PEEREVAL = 13;
public static final int BREAK = 14;

// sakaiId used for an item copied from another site with no real content
public static final String DUMMY = "/dummy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ public class PageData {
// so normally this should be done using appropriate functions from SimplePageBean.
public boolean deleteItem(Object o);

// just delete, no permissions checking and no logging
public boolean quickDelete(Object o);

// see saveItem for details and caveats, same function except update instead of save
public boolean update(Object o, List<String> elist, String nowriteerr, boolean requiresEditPermission);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,27 @@ public boolean deleteItem(Object o) {
}
}

public boolean quickDelete(Object o) {
try {
getHibernateTemplate().delete(o);
return true;
} catch (DataAccessException e) {
try {

/* If we have multiple objects of the same item, you must merge them
* before deleting. If the first delete fails, we merge and try again.
*/
getHibernateTemplate().delete(getHibernateTemplate().merge(o));

return true;
}catch(DataAccessException ex) {
ex.printStackTrace();
log.warn("Hibernate could not delete: " + e.toString());
return false;
}
}
}

public boolean update(Object o, List<String>elist, String nowriteerr, boolean requiresEditPermission) {
/*
* This checks a lot of conditions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class SimplePageItemImpl implements SimplePageItem {
public static final int QUESTION = 11;
public static final int BLTI = 12;
public static final int PEEREVAL = 13;
public static final int BREAK = 14;

// must agree with definition in hbm file
public static final int MAXNAME = 100;
Expand All @@ -77,6 +78,7 @@ public class SimplePageItemImpl implements SimplePageItem {
private String alt;
private boolean nextPage; // show as next rather than subpage
private String format; // display format, currently nothing, button or maybe li
// for breaks, "section" or "column"
private boolean required;
private boolean alternate; // student can do this or the one above
private boolean subrequirement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,9 @@ else if (!multiplenext) {
case SimplePageItem.PEEREVAL:
itemString = messageLocator.getMessage("simplepage.peerEval-secotion");
break;
case SimplePageItem.BREAK:
itemString = messageLocator.getMessage("simplepage.break");
break;
}
errStream.println(messageLocator.getMessage("simplepage.exportcc-bad-type").replace("{1}", title).replace("{2}", item.getName()).replace("{3}", itemString));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,18 @@ public static String groupErrors(String siteId, String locale, String groupStrin
}


public static String toggleGrouped(String itemId, String csrfToken) {
public static String insertBreakBefore(String itemId, String type, String cols, String csrfToken) {

if (itemId == null) {
log.error("Ajax togglegrouped passed null itemid");
log.error("Ajax insertBreakBefore passed null itemid");
return null;
}

if (!"section".equals(type) && !"column".equals(type)) {
log.error("Ajax insertBreakBefore passed illegal type " + type);
return null;
}

itemId = itemId.trim();

// currently this is only needed by the instructor
Expand All @@ -484,32 +489,106 @@ public static String toggleGrouped(String itemId, String csrfToken) {
siteId = page.getSiteId();
} catch (Exception e) {
e.printStackTrace();
log.error("Ajax togglegrouped passed invalid data " + e);
log.error("Ajax insertBreakBefore passed invalid data " + e);
return null;
}
if (siteId == null) {
log.error("Ajax togglegrouped passed null site id");
log.error("Ajax insertBreakBefore passed null site id");
return null;
}

String ref = "/site/" + siteId;
if (!SecurityService.unlock(SimplePage.PERMISSION_LESSONBUILDER_UPDATE, ref) || !checkCsrf(csrfToken)) {
log.error("Ajax togglegrouped passed itemid " + itemId + " but user doesn't have permission");
log.error("Ajax insertBreakBefore passed itemid " + itemId + " but user doesn't have permission");
return null;
}

String grouped = item.getAttribute("groupedWithBelow");
if ("true".equals(grouped)) {
item.removeAttribute("groupedWithBelow");
grouped = "false";
} else {
item.setAttribute("groupedWithBelow", "true");
grouped = "true";
List<SimplePageItem>items = simplePageToolDao.findItemsOnPage(item.getPageId());

// we have an item id. insert before it
int nseq = 0; // sequence number of new item
boolean after = false; // we found the item to insert before
// have an item number specified, look for the item to insert before
long before = item.getId();
for (SimplePageItem i: items) {
if (i.getId() == before) {
// found item to insert before
// use its sequence and bump up it and all after
nseq = i.getSequence();
after = true;
}
if (after) {
i.setSequence(i.getSequence() + 1);
simplePageToolDao.quickUpdate(i);
}
}

// if after not set, we didn't find the item; either no item specified or it
if (!after) {
log.error("Ajax insertBreakBefore passed item not on its page " + before);
return null;
}

SimplePageItem i = simplePageToolDao.makeItem(item.getPageId(), nseq, SimplePageItem.BREAK, null, null);
i.setFormat(type);

simplePageToolDao.quickSaveItem(i);
return "" + i.getId();

simplePageToolDao.quickUpdate(item);
}

public static String deleteItem(String itemId, String csrfToken) {
if (itemId == null) {
log.error("Ajax deleteBreak passed null itemid");
return null;
}

itemId = itemId.trim();

// currently this is only needed by the instructor

SimplePageItem item = null;
SimplePage page = null;
String siteId = null;
try {
item = simplePageToolDao.findItem(Long.parseLong(itemId));
page = simplePageToolDao.getPage(item.getPageId());
siteId = page.getSiteId();
} catch (Exception e) {
e.printStackTrace();
log.error("Ajax deleteBreak passed invalid data " + e);
return null;
}
if (siteId == null) {
log.error("Ajax deleteBreak passed null site id");
return null;
}

String ref = "/site/" + siteId;
if (!SecurityService.unlock(SimplePage.PERMISSION_LESSONBUILDER_UPDATE, ref) || !checkCsrf(csrfToken)) {
log.error("Ajax deleteBreak passed itemid " + itemId + " but user doesn't have permission");
return null;
}

List<SimplePageItem>items = simplePageToolDao.findItemsOnPage(item.getPageId());

// we have an item id. adjust sequence for items after it
boolean after = false; // we found the item to delete
// have an item number specified, look for it
long before = item.getId();
for (SimplePageItem i: items) {
if (item.getId() == before) {
after = true;
} else if (after) {
item.setSequence(item.getSequence() - 1);
simplePageToolDao.quickUpdate(item);
}
}

simplePageToolDao.quickDelete(item);

return "ok";

return grouped;
}

public static boolean checkCsrf(String csrfToken) {
Expand Down Expand Up @@ -560,11 +639,18 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws Ser
String locale = req.getParameter("locale");
String groups = req.getParameter("groups");
out.print(groupErrors(siteid, locale, groups));
} else if (op.equals("togglegrouped")) {
} else if (op.equals("insertbreakbefore")) {
String itemId = req.getParameter("itemid");
String type = req.getParameter("type");
String cols = req.getParameter("cols");
String csrfToken = req.getParameter("csrf");
out.println(toggleGrouped(itemId, csrfToken));
out.println(insertBreakBefore(itemId, type, cols, csrfToken));
} else if (op.equals("deleteitem")) {
String itemId = req.getParameter("itemid");
String csrfToken = req.getParameter("csrf");
out.println(deleteItem(itemId, csrfToken));
}

}

public void setMessageSource(MessageSource s) {
Expand Down
Loading

0 comments on commit 069a96d

Please sign in to comment.