Skip to content

Commit

Permalink
LSNBLDR-877 use Hibernate and siteService to retrieve instead of bad …
Browse files Browse the repository at this point in the history
…custom SQL (sakaiproject#4858)
  • Loading branch information
ottenhoff authored Oct 4, 2017
1 parent a56891f commit 3b39981
Showing 1 changed file with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.sakaiproject.db.api.SqlReader;
import org.sakaiproject.db.api.SqlService;
import org.sakaiproject.event.cover.EventTrackingService;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.lessonbuildertool.api.LessonBuilderEvents;
import org.sakaiproject.lessonbuildertool.ChecklistItemStatus;
import org.sakaiproject.lessonbuildertool.SimpleChecklistItem;
Expand Down Expand Up @@ -80,6 +81,7 @@
import org.sakaiproject.lessonbuildertool.SimpleStudentPageImpl;
import org.sakaiproject.lessonbuildertool.api.LessonBuilderConstants;
import org.sakaiproject.lessonbuildertool.util.LessonsSubNavBuilder;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.api.SitePage;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.site.api.ToolConfiguration;
Expand All @@ -104,7 +106,6 @@ public class SimplePageToolDaoImpl extends HibernateDaoSupport implements Simple
private SiteService siteService;
private SqlService sqlService;
private AuthzGroupService authzGroupService;
private static String SITE_UPD = "site.upd";

// part of HibernateDaoSupport; this is the only context in which it is OK
// to modify the template configuration
Expand Down Expand Up @@ -209,19 +210,39 @@ public void clear() {
}

public List<SimplePageItem> findItemsInSite(String siteId) {
Object [] fields = new Object[1];
fields[0] = siteId;
List<String> ids = sqlService.dbRead("select b.id from lesson_builder_pages a,lesson_builder_items b,SAKAI_SITE_PAGE c where a.siteId = ? and a.parent is null and a.pageId = b.sakaiId and b.type = 2 and b.pageId = 0 and a.toolId = c.PAGE_ID order by c.SITE_ORDER", fields, null);
// This is a three-step process.
// 1) Find the pages in the site
// 2) Find the Lessons pages
// 3) Find the Lessons items
try {
Site site = siteService.getSite(siteId);
List<SitePage> sitePages = site.getOrderedPages();

List<SimplePageItem> result = new ArrayList<SimplePageItem>();

if (result != null) {
for (String id: ids) {
SimplePageItem i = findItem(new Long(id));
result.add(i);
List<String> sitePageIds = new ArrayList<>();
for (SitePage page : sitePages) {
sitePageIds.add(page.getId());
}

DetachedCriteria d = DetachedCriteria.forClass(SimplePage.class);
d.add(Restrictions.in("toolId", sitePageIds));
d.add(Restrictions.isNull("parent"));

List<SimplePage> lessonsPages = (List<SimplePage>) getHibernateTemplate().findByCriteria(d);
List<String> lessonsPageIds = new ArrayList<>();
if (!lessonsPages.isEmpty()) {
for (SimplePage lessonsPage : lessonsPages) {
String pageId = String.valueOf(lessonsPage.getPageId());
lessonsPageIds.add(pageId);
}

List<SimplePageItem> pageItems = findTopLevelPageItemsBySakaiIds(lessonsPageIds);
return pageItems;
}
} catch (IdUnusedException e) {
log.warn("Could not find site: " + siteId, e);
}
}
return result;

return null;
}

public List<SimplePageItem> findDummyItemsInSite(String siteId) {
Expand Down Expand Up @@ -499,6 +520,21 @@ public SimplePageItem findTopLevelPageItemBySakaiId(String id) {
return list.get(0);
}

public List<SimplePageItem> findTopLevelPageItemsBySakaiIds(List<String> ids) {
DetachedCriteria d = DetachedCriteria.forClass(SimplePageItem.class)
.add(Restrictions.in("sakaiId", ids))
.add(Restrictions.eq("pageId", 0L))
.add(Restrictions.eq("type",SimplePageItem.PAGE));

List<SimplePageItem> list = (List<SimplePageItem>) getHibernateTemplate().findByCriteria(d);

if (list == null || list.size() < 1) {
return null;
}

return list;
}

public List<SimplePageItem> findPageItemsBySakaiId(String id) {
DetachedCriteria d = DetachedCriteria.forClass(SimplePageItem.class).add(Restrictions.eq("sakaiId", id)).
add(Restrictions.eq("type",SimplePageItem.PAGE));
Expand Down

0 comments on commit 3b39981

Please sign in to comment.