Index: lams_central/src/java/org/lamsfoundation/lams/web/qb/QbCollectionController.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/web/qb/QbCollectionController.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/web/qb/QbCollectionController.java (revision 8b28898e77f3b0abafb08346c55fae0c590cad96) @@ -0,0 +1,151 @@ +/**************************************************************** + * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org) + * ============================================================= + * License Information: http://lamsfoundation.org/licensing/lams/2.0/ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2.0 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA + * + * http://www.gnu.org/licenses/gpl.txt + * **************************************************************** + */ + +package org.lamsfoundation.lams.web.qb; + +import java.io.StringWriter; +import java.util.List; + +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.apache.log4j.Logger; +import org.lamsfoundation.lams.qb.model.QbCollection; +import org.lamsfoundation.lams.qb.model.QbQuestion; +import org.lamsfoundation.lams.qb.service.IQbService; +import org.lamsfoundation.lams.usermanagement.dto.UserDTO; +import org.lamsfoundation.lams.util.CommonConstants; +import org.lamsfoundation.lams.util.MessageService; +import org.lamsfoundation.lams.web.session.SessionManager; +import org.lamsfoundation.lams.web.util.AttributeNames; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +@Controller +@RequestMapping("/qb/collection") +public class QbCollectionController { + private static Logger log = Logger.getLogger(QbCollectionController.class); + + @Autowired + @Qualifier("centralMessageService") + private MessageService messageService; + + @Autowired + private IQbService qbService; + + @RequestMapping("/show") + public String showUserCollections(Model model) throws Exception { + Integer userId = getUserId(); + List collections = qbService.getUserCollections(userId); + + QbCollection privateCollection = qbService.getUserPrivateCollection(userId); + collections.add(privateCollection); + + QbCollection publicCollection = qbService.getPublicCollection(); + collections.add(publicCollection); + + model.addAttribute("collections", collections); + return "qb/collection"; + } + + @RequestMapping("/getCollectionGridData") + @ResponseBody + public String getCollectionGridData(@RequestParam long collectionUid, HttpServletResponse response) { + response.setContentType("text/xml; charset=utf-8"); + + List questions = qbService.getCollectionQuestions(collectionUid, 0, 10); + return QbCollectionController.toGridXML(questions); + } + + private static String toGridXML(List questions) { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document document = builder.newDocument(); + + // root element + Element rootElement = document.createElement(CommonConstants.ELEMENT_ROWS); + + Element pageElement = document.createElement(CommonConstants.ELEMENT_PAGE); + pageElement.appendChild(document.createTextNode("" + 1)); + rootElement.appendChild(pageElement); + + Element totalPageElement = document.createElement(CommonConstants.ELEMENT_TOTAL); + totalPageElement.appendChild(document.createTextNode("" + 1)); + rootElement.appendChild(totalPageElement); + + Element recordsElement = document.createElement(CommonConstants.ELEMENT_RECORDS); + recordsElement.appendChild(document.createTextNode("" + questions.size())); + rootElement.appendChild(recordsElement); + + for (QbQuestion question : questions) { + Element rowElement = document.createElement(CommonConstants.ELEMENT_ROW); + rowElement.setAttribute(CommonConstants.ELEMENT_ID, question.getUid().toString()); + + String[] data = { question.getUid().toString(), question.getName(), question.getUid().toString() }; + + for (String cell : data) { + Element cellElement = document.createElement(CommonConstants.ELEMENT_CELL); + if (cell == null) { + cell = ""; + } + cellElement.appendChild(document.createTextNode(cell)); + rowElement.appendChild(cellElement); + } + rootElement.appendChild(rowElement); + } + + document.appendChild(rootElement); + DOMSource domSource = new DOMSource(document); + StringWriter writer = new StringWriter(); + StreamResult result = new StreamResult(writer); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.transform(domSource, result); + return writer.toString(); + + } catch (Exception e) { + log.error("Error while generating Question Bank collection jqGrid XML data", e); + } + + return null; + } + + private Integer getUserId() { + HttpSession ss = SessionManager.getSession(); + UserDTO user = (UserDTO) ss.getAttribute(AttributeNames.USER); + return user != null ? user.getUserID() : null; + } +} \ No newline at end of file Index: lams_central/web/qb/collection.jsp =================================================================== diff -u --- lams_central/web/qb/collection.jsp (revision 0) +++ lams_central/web/qb/collection.jsp (revision 8b28898e77f3b0abafb08346c55fae0c590cad96) @@ -0,0 +1,77 @@ +<%@ page contentType="text/html; charset=utf-8" language="java"%> +<%@ taglib uri="tags-lams" prefix="lams"%> +<%@ taglib uri="tags-fmt" prefix="fmt"%> +<%@ taglib uri="tags-core" prefix="c"%> + + + + + Question collections + + + + + + + + + + + + + + +
+
+
+
+
+ +
\ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/qb/dao/hibernate/QbDAO.java =================================================================== diff -u -re08a9e538cf4c89f50fd15cdabfa0319076c2692 -r8b28898e77f3b0abafb08346c55fae0c590cad96 --- lams_common/src/java/org/lamsfoundation/lams/qb/dao/hibernate/QbDAO.java (.../QbDAO.java) (revision e08a9e538cf4c89f50fd15cdabfa0319076c2692) +++ lams_common/src/java/org/lamsfoundation/lams/qb/dao/hibernate/QbDAO.java (.../QbDAO.java) (revision 8b28898e77f3b0abafb08346c55fae0c590cad96) @@ -47,7 +47,7 @@ + "GROUP BY b.question ORDER BY COUNT(bl.uid) DESC"; private static final String FIND_COLLECTION_QUESTIONS = "SELECT q.* FROM lams_qb_collection_question AS c " - + "JOIN lams_qb_question ON c.qb_question_uid = q.uid WHERE c.collection_uid = :collectionUid"; + + "JOIN lams_qb_question AS q ON c.qb_question_uid = q.uid WHERE c.collection_uid = :collectionUid"; private static final String ADD_COLLECTION_QUESTION = "INSERT INTO lams_qb_collection_question VALUES (:collectionUid, :qbQuestionUid)"; Index: lams_common/src/java/org/lamsfoundation/lams/qb/model/QbCollection.java =================================================================== diff -u -re08a9e538cf4c89f50fd15cdabfa0319076c2692 -r8b28898e77f3b0abafb08346c55fae0c590cad96 --- lams_common/src/java/org/lamsfoundation/lams/qb/model/QbCollection.java (.../QbCollection.java) (revision e08a9e538cf4c89f50fd15cdabfa0319076c2692) +++ lams_common/src/java/org/lamsfoundation/lams/qb/model/QbCollection.java (.../QbCollection.java) (revision 8b28898e77f3b0abafb08346c55fae0c590cad96) @@ -14,6 +14,7 @@ import javax.persistence.ManyToMany; import javax.persistence.OrderBy; import javax.persistence.Table; +import javax.persistence.Transient; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; Index: lams_common/src/java/org/lamsfoundation/lams/qb/service/IQbService.java =================================================================== diff -u -re08a9e538cf4c89f50fd15cdabfa0319076c2692 -r8b28898e77f3b0abafb08346c55fae0c590cad96 --- lams_common/src/java/org/lamsfoundation/lams/qb/service/IQbService.java (.../IQbService.java) (revision e08a9e538cf4c89f50fd15cdabfa0319076c2692) +++ lams_common/src/java/org/lamsfoundation/lams/qb/service/IQbService.java (.../IQbService.java) (revision 8b28898e77f3b0abafb08346c55fae0c590cad96) @@ -59,6 +59,8 @@ List getUserCollections(int userId); + List getCollectionQuestions(long collectionUid, Integer offset, Integer limit); + QbCollection addCollection(int userId, String name); void removeCollection(long collectionUid); Index: lams_common/src/java/org/lamsfoundation/lams/qb/service/QbService.java =================================================================== diff -u -re08a9e538cf4c89f50fd15cdabfa0319076c2692 -r8b28898e77f3b0abafb08346c55fae0c590cad96 --- lams_common/src/java/org/lamsfoundation/lams/qb/service/QbService.java (.../QbService.java) (revision e08a9e538cf4c89f50fd15cdabfa0319076c2692) +++ lams_common/src/java/org/lamsfoundation/lams/qb/service/QbService.java (.../QbService.java) (revision 8b28898e77f3b0abafb08346c55fae0c590cad96) @@ -264,10 +264,18 @@ @Override @SuppressWarnings("unchecked") public List getUserCollections(int userId) { - return qbDAO.findByProperty(QbCollection.class, "user.userId", userId); + Map map = new HashMap<>(); + map.put("userId", userId); + map.put("personal", false); + return qbDAO.findByProperties(QbCollection.class, map); } @Override + public List getCollectionQuestions(long collectionUid, Integer offset, Integer limit) { + return qbDAO.getCollectionQuestions(collectionUid, offset, limit); + } + + @Override public QbCollection addCollection(int userId, String name) { QbCollection collection = new QbCollection(); collection.setName(name);