Source Code Cross Referenced for QuestionPoolFacadeQueries.java in  » ERP-CRM-Financial » sakai » org » sakaiproject » tool » assessment » facade » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ERP CRM Financial » sakai » org.sakaiproject.tool.assessment.facade 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**********************************************************************************
0002:         * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/QuestionPoolFacadeQueries.java $
0003:         * $Id: QuestionPoolFacadeQueries.java 9343 2006-05-12 23:30:02Z lydial@stanford.edu $
0004:         ***********************************************************************************
0005:         *
0006:         * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
0007:         *
0008:         * Licensed under the Educational Community License, Version 1.0 (the"License");
0009:         * you may not use this file except in compliance with the License.
0010:         * You may obtain a copy of the License at
0011:         *
0012:         *      http://www.opensource.org/licenses/ecl1.php
0013:         *
0014:         * Unless required by applicable law or agreed to in writing, software
0015:         * distributed under the License is distributed on an "AS IS" BASIS,
0016:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0017:         * See the License for the specific language governing permissions and
0018:         * limitations under the License.
0019:         *
0020:         **********************************************************************************/package org.sakaiproject.tool.assessment.facade;
0021:
0022:        import java.sql.SQLException;
0023:        import java.util.ArrayList;
0024:        import java.util.Collection;
0025:        import java.util.Date;
0026:        import java.util.HashMap;
0027:        import java.util.HashSet;
0028:        import java.util.Iterator;
0029:        import java.util.List;
0030:        import java.util.Set;
0031:        import java.util.Vector;
0032:        import java.util.Collections;
0033:
0034:        import org.hibernate.HibernateException;
0035:        import org.hibernate.Query;
0036:        import org.hibernate.Session;
0037:
0038:        import org.apache.commons.logging.Log;
0039:        import org.apache.commons.logging.LogFactory;
0040:        import org.sakaiproject.tool.assessment.data.model.Tree;
0041:        import org.sakaiproject.tool.assessment.data.dao.assessment.Answer;
0042:        import org.sakaiproject.tool.assessment.data.dao.assessment.AnswerFeedback;
0043:        import org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment;
0044:        import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
0045:        import org.sakaiproject.tool.assessment.data.dao.assessment.ItemMetaData;
0046:        import org.sakaiproject.tool.assessment.data.dao.assessment.ItemText;
0047:        import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemAttachmentIfc;
0048:        import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
0049:        import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemMetaDataIfc;
0050:        import org.sakaiproject.tool.assessment.data.dao.questionpool.QuestionPoolAccessData;
0051:        import org.sakaiproject.tool.assessment.data.dao.questionpool.QuestionPoolData;
0052:        import org.sakaiproject.tool.assessment.data.dao.questionpool.QuestionPoolItemData;
0053:        import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl;
0054:        import org.springframework.dao.DataAccessException;
0055:        import org.springframework.orm.hibernate3.HibernateCallback;
0056:        import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
0057:        import org.sakaiproject.tool.assessment.services.ItemService;
0058:        import org.sakaiproject.tool.assessment.services.PersistenceService;
0059:
0060:        public class QuestionPoolFacadeQueries extends HibernateDaoSupport
0061:                implements  QuestionPoolFacadeQueriesAPI {
0062:            private static Log log = LogFactory
0063:                    .getLog(QuestionPoolFacadeQueries.class);
0064:
0065:            public QuestionPoolFacadeQueries() {
0066:            }
0067:
0068:            public IdImpl getQuestionPoolId(String id) {
0069:                return new IdImpl(id);
0070:            }
0071:
0072:            public IdImpl getQuestionPoolId(Long id) {
0073:                return new IdImpl(id);
0074:            }
0075:
0076:            public IdImpl getQuestionPoolId(long id) {
0077:                return new IdImpl(id);
0078:            }
0079:
0080:            /**
0081:             * Get a list of all the pools in the site. Note that questions in each pool will not
0082:             * be populated. We must keep this list updated.
0083:             */
0084:            public List getAllPools() {
0085:                return getHibernateTemplate().find("from QuestionPoolData");
0086:            }
0087:
0088:            public List getAllPoolsByAgent(final String agentId) {
0089:                final HibernateCallback hcb = new HibernateCallback() {
0090:                    public Object doInHibernate(Session session)
0091:                            throws HibernateException, SQLException {
0092:                        Query q = session
0093:                                .createQuery("from QuestionPoolData a where a.ownerId= ? ");
0094:                        q.setString(0, agentId);
0095:                        return q.list();
0096:                    };
0097:                };
0098:                List list = getHibernateTemplate().executeFind(hcb);
0099:
0100:                //	  List list = getHibernateTemplate().find(
0101:                //        "from QuestionPoolData a where a.ownerId= ? ",
0102:                //        new Object[] {agentId}
0103:                //        , new org.hibernate.type.Type[] {Hibernate.STRING});
0104:                return list;
0105:
0106:            }
0107:
0108:            /**
0109:             * Get all the pools that the agent has access to. The easiest way seems to be
0110:             * #1. get all the existing pool
0111:             * #2. get all the QuestionPoolAccessData record of the agent
0112:             * #3. go through the existing pools and check it against the QuestionPoolAccessData (qpa) record to see if
0113:             * the agent is granted access to it. qpa record (if exists) always trumps the default access right set
0114:             * up for a pool
0115:             * e.g. if the defaultAccessType for a pool is ACCESS_DENIED but the qpa record say ADMIN, then access=ADMIN
0116:             * e.g. if the defaultAccessType for a pool is ADMIN but the qpa record say ACCESS_DENIED, then access=ACCESS_DENIED
0117:             * e.g. if no qpa record exists, then access rule will follow the defaultAccessType set by the pool
0118:             */
0119:            public QuestionPoolIteratorFacade getAllPools(String agentId) {
0120:                ArrayList qpList = new ArrayList();
0121:
0122:                // #1.
0123:                // lydial: 9/22/05 we are not really using QuestionPoolAccessData, so filter by ownerid 
0124:                //List poolList = getAllPools(); 
0125:                List poolList = getAllPoolsByAgent(agentId);
0126:
0127:                /*
0128:                 // #2. get all the QuestionPoolAccessData record belonging to the agent
0129:                 List questionPoolAccessList = getHibernateTemplate().find(
0130:                 "from QuestionPoolAccessData as qpa where qpa.agentId=?",
0131:                 new Object[] {agentId}
0132:                 , new org.hibernate.type.Type[] {Hibernate.STRING});
0133:                 HashMap h = new HashMap(); // prepare a hashMap with (poolId, qpa)
0134:                 Iterator i = questionPoolAccessList.iterator();
0135:                 while (i.hasNext()) {
0136:                 QuestionPoolAccessData qpa = (QuestionPoolAccessData) i.next();
0137:                 h.put(qpa.getQuestionPoolId(), qpa);
0138:                 }
0139:
0140:                 // #3. We need to go through the existing QuestionPool and the QuestionPoolAccessData record
0141:                 // to determine the access type
0142:                 */
0143:
0144:                try {
0145:                    Iterator j = poolList.iterator();
0146:                    while (j.hasNext()) {
0147:                        QuestionPoolData qpp = (QuestionPoolData) j.next();
0148:                        // I really wish we don't need to populate  the questionpool size & subpool size for JSF
0149:                        // watch this for performance. hope Hibernate is smart enough not to load the entire question
0150:                        // - daisy, 10/04/04
0151:                        populateQuestionPoolItemDatas(qpp);
0152:
0153:                        qpList.add(getQuestionPool(qpp));
0154:                        /*
0155:                         QuestionPoolAccessData qpa = (QuestionPoolAccessData) h.get(qpp.
0156:                         getQuestionPoolId());
0157:                         if (qpa == null) {
0158:                         // if (qpa == null), take what is set for pool.
0159:                         if (! (QuestionPoolFacade.ACCESS_DENIED).equals(qpp.getAccessTypeId())) {
0160:                         qpList.add(getQuestionPool(qpp));
0161:                         }
0162:                         }
0163:                         else {
0164:                         if (! (QuestionPoolFacade.ACCESS_DENIED).equals(qpa.getAccessTypeId())) {
0165:                         qpp.setAccessTypeId(qpa.getAccessTypeId());
0166:                         qpList.add(getQuestionPool(qpp));
0167:                         }
0168:                         }
0169:                         */
0170:                    }
0171:                } catch (Exception e) {
0172:                    log.warn(e.getMessage());
0173:                }
0174:                return new QuestionPoolIteratorFacade(qpList);
0175:            }
0176:
0177:            public ArrayList getBasicInfoOfAllPools(final String agentId) {
0178:                final HibernateCallback hcb = new HibernateCallback() {
0179:                    public Object doInHibernate(Session session)
0180:                            throws HibernateException, SQLException {
0181:                        Query q = session
0182:                                .createQuery("select new QuestionPoolData(a.questionPoolId, a.title)from QuestionPoolData a where a.ownerId= ? ");
0183:                        q.setString(0, agentId);
0184:                        return q.list();
0185:                    };
0186:                };
0187:                List list = getHibernateTemplate().executeFind(hcb);
0188:
0189:                //	  List list = getHibernateTemplate().find(
0190:                //        "select new QuestionPoolData(a.questionPoolId, a.title)from QuestionPoolData a where a.ownerId= ? ",
0191:                //        new Object[] {agentId}
0192:                //        , new org.hibernate.type.Type[] {Hibernate.STRING});
0193:                ArrayList poolList = new ArrayList();
0194:                for (int i = 0; i < list.size(); i++) {
0195:                    QuestionPoolData a = (QuestionPoolData) list.get(i);
0196:                    QuestionPoolFacade f = new QuestionPoolFacade(a
0197:                            .getQuestionPoolId(), a.getTitle());
0198:                    poolList.add(f);
0199:                }
0200:                return poolList;
0201:            }
0202:
0203:            private QuestionPoolFacade getQuestionPool(QuestionPoolData qpp) {
0204:                try {
0205:                    return new QuestionPoolFacade(qpp);
0206:                } catch (Exception e) {
0207:                    log.warn(e.getMessage());
0208:                    return null;
0209:                }
0210:            }
0211:
0212:            private List getAllItemsInThisPoolOnlyAndDetachFromAssessment(
0213:                    final Long questionPoolId) {
0214:                // return items that belong to this pool and this pool only.  These items can not be part of any assessment either.
0215:                List list = getAllItemsInThisPoolOnly(questionPoolId);
0216:                ArrayList newlist = new ArrayList();
0217:                for (int i = 0; i < list.size(); i++) {
0218:                    ItemData itemdata = (ItemData) list.get(i);
0219:                    if (itemdata.getSection() == null) {
0220:                        // these items do not belong to any assessments, so add them to the list
0221:                        newlist.add(itemdata);
0222:                    } else {
0223:                        // do not add these items to the list, but we need to remove the POOLID metadata
0224:
0225:                        // this item still links to an assessment 
0226:                        // remove this item's POOLID itemmetadata
0227:                        itemdata.removeMetaDataByType(ItemMetaDataIfc.POOLID);
0228:                        getHibernateTemplate().saveOrUpdate(itemdata); //save itemdata after removing metadata 
0229:                    }
0230:                }
0231:                return newlist;
0232:            }
0233:
0234:            private List getAllItemsInThisPoolOnly(final Long questionPoolId) {
0235:                // return items that belong to this pool and this pool only.  
0236:                final HibernateCallback hcb = new HibernateCallback() {
0237:                    public Object doInHibernate(Session session)
0238:                            throws HibernateException, SQLException {
0239:                        Query q = session
0240:                                .createQuery("select ab from ItemData ab, QuestionPoolItemData qpi where ab.itemId=qpi.itemId and qpi.questionPoolId = ?");
0241:                        q.setLong(0, questionPoolId.longValue());
0242:                        return q.list();
0243:                    };
0244:                };
0245:                List list = getHibernateTemplate().executeFind(hcb);
0246:
0247:                ArrayList newlist = new ArrayList();
0248:                for (int i = 0; i < list.size(); i++) {
0249:                    ItemData itemdata = (ItemData) list.get(i);
0250:                    String itemId = itemdata.getItemId().toString();
0251:                    if (getPoolIdsByItem(itemId).size() == 1) {
0252:                        newlist.add(itemdata);
0253:                    } else {
0254:                        // this item still links to other pool(s)
0255:                    }
0256:                }
0257:                return newlist;
0258:            }
0259:
0260:            public List getAllItems(final Long questionPoolId) {
0261:                final HibernateCallback hcb = new HibernateCallback() {
0262:                    public Object doInHibernate(Session session)
0263:                            throws HibernateException, SQLException {
0264:                        Query q = session
0265:                                .createQuery("select ab from ItemData ab, QuestionPoolItemData qpi where ab.itemId=qpi.itemId and qpi.questionPoolId = ?");
0266:                        q.setLong(0, questionPoolId.longValue());
0267:                        return q.list();
0268:                    };
0269:                };
0270:                List list = getHibernateTemplate().executeFind(hcb);
0271:
0272:                //    List list = getHibernateTemplate().find("select ab from ItemData ab, QuestionPoolItemData qpi where ab.itemId=qpi.itemId and qpi.questionPoolId = ?",
0273:                //                                            new Object[] {questionPoolId}
0274:                //                                            ,
0275:                //                                            new org.hibernate.type.Type[] {Hibernate.
0276:                //                                            LONG});
0277:                return list;
0278:
0279:            }
0280:
0281:            public List getAllItemFacadesOrderByItemText(
0282:                    final Long questionPoolId, final String orderBy,
0283:                    final String ascending) {
0284:
0285:                // Fixed for bug 3559
0286:                log
0287:                        .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: orderBy="
0288:                                + orderBy);
0289:                List list = getAllItems(questionPoolId);
0290:
0291:                log
0292:                        .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: size = "
0293:                                + list.size());
0294:                HashMap hp = new HashMap();
0295:                Vector origValueV;
0296:                ItemData itemData;
0297:                ItemFacade itemFacade;
0298:                Vector facadeVector = new Vector();
0299:                String text;
0300:                for (int i = 0; i < list.size(); i++) {
0301:                    log
0302:                            .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: i = "
0303:                                    + i);
0304:                    itemData = (ItemData) list.get(i);
0305:                    itemFacade = new ItemFacade(itemData);
0306:                    facadeVector.add(itemFacade);
0307:                    log
0308:                            .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: getItemId = "
0309:                                    + itemData.getItemId());
0310:                    log
0311:                            .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: getText = "
0312:                                    + itemData.getText());
0313:                    text = itemFacade.getTextHtmlStrippedAll();
0314:                    log
0315:                            .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: getTextHtmlStrippedAll = '"
0316:                                    + text + "'");
0317:
0318:                    origValueV = (Vector) hp.get(text);
0319:                    if (origValueV == null) {
0320:                        log
0321:                                .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: origValueV is null ");
0322:                        origValueV = new Vector();
0323:                    }
0324:                    origValueV.add(new Integer(i));
0325:                    hp.put(text, origValueV);
0326:                }
0327:
0328:                Vector v = new Vector(hp.keySet());
0329:                Collections.sort(v, String.CASE_INSENSITIVE_ORDER);
0330:                ArrayList itemList = new ArrayList();
0331:
0332:                Iterator it = v.iterator();
0333:                Vector orderdValueV;
0334:                Integer value;
0335:                String key;
0336:                if ((ascending != null) && ("false").equals(ascending)) {//sort descending
0337:                    for (int l = v.size() - 1; l >= 0; l--) {
0338:                        key = (String) v.get(l);
0339:                        orderdValueV = (Vector) hp.get(key);
0340:                        Iterator iter = orderdValueV.iterator();
0341:                        while (iter.hasNext()) {
0342:                            value = (Integer) iter.next();
0343:
0344:                            ItemData itemdata = (ItemData) list.get(value
0345:                                    .intValue());
0346:                            ItemFacade f = new ItemFacade(itemdata);
0347:                            itemList.add(f);
0348:                        }
0349:                    }
0350:                } else {//sort ascending
0351:                    while (it.hasNext()) {
0352:                        key = (String) it.next();
0353:                        orderdValueV = (Vector) hp.get(key);
0354:                        Iterator iter = orderdValueV.iterator();
0355:                        while (iter.hasNext()) {
0356:                            value = (Integer) iter.next();
0357:                            log
0358:                                    .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemText:: sorted (value) = "
0359:                                            + value);
0360:                            itemFacade = (ItemFacade) facadeVector.get(value
0361:                                    .intValue());
0362:                            itemList.add(itemFacade);
0363:                        }
0364:                    }
0365:                }
0366:                return itemList;
0367:            }
0368:
0369:            public List getAllItemFacadesOrderByItemType(
0370:                    final Long questionPoolId, final String orderBy,
0371:                    final String ascending) {
0372:                log
0373:                        .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemType:: orderBy="
0374:                                + orderBy);
0375:                final HibernateCallback hcb = new HibernateCallback() {
0376:                    public Object doInHibernate(Session session)
0377:                            throws HibernateException, SQLException {
0378:                        Query q;
0379:                        if ("false".equals(ascending)) {
0380:                            q = session
0381:                                    .createQuery("select ab from ItemData ab, QuestionPoolItemData qpi, TypeD t where ab.itemId=qpi.itemId and ab.typeId=t.typeId and qpi.questionPoolId = ? order by t."
0382:                                            + orderBy + " desc");
0383:                        } else {
0384:                            q = session
0385:                                    .createQuery("select ab from ItemData ab, QuestionPoolItemData qpi, TypeD t where ab.itemId=qpi.itemId and ab.typeId=t.typeId and qpi.questionPoolId = ? order by t."
0386:                                            + orderBy);
0387:                        }
0388:
0389:                        q.setLong(0, questionPoolId.longValue());
0390:                        log
0391:                                .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemType:: getQueryString() = "
0392:                                        + q.getQueryString());
0393:                        return q.list();
0394:                    };
0395:                };
0396:                List list = getHibernateTemplate().executeFind(hcb);
0397:
0398:                //    List list = getHibernateTemplate().find("select ab from ItemData ab, QuestionPoolItemData qpi, TypeD t where ab.itemId=qpi.itemId and ab.typeId=t.typeId and qpi.questionPoolId = ? order by t." +
0399:                //                                            orderBy,
0400:                //                                            new Object[] {questionPoolId}
0401:                //                                            ,
0402:                //                                            new org.hibernate.type.Type[] {Hibernate.
0403:                //                                            LONG});
0404:                log
0405:                        .debug("QuestionPoolFacadeQueries: getAllItemFacadesOrderByItemType:: size = "
0406:                                + list.size());
0407:                ArrayList itemList = new ArrayList();
0408:                for (int i = 0; i < list.size(); i++) {
0409:                    ItemData itemdata = (ItemData) list.get(i);
0410:                    ItemFacade f = new ItemFacade(itemdata);
0411:                    itemList.add(f);
0412:                }
0413:                return itemList;
0414:            }
0415:
0416:            public List getAllItemFacades(final Long questionPoolId) {
0417:                final HibernateCallback hcb = new HibernateCallback() {
0418:                    public Object doInHibernate(Session session)
0419:                            throws HibernateException, SQLException {
0420:                        Query q = session
0421:                                .createQuery("select ab from ItemData ab, QuestionPoolItemData qpi where ab.itemId=qpi.itemId and qpi.questionPoolId = ?");
0422:                        q.setLong(0, questionPoolId.longValue());
0423:                        return q.list();
0424:                    };
0425:                };
0426:                List list = getHibernateTemplate().executeFind(hcb);
0427:
0428:                //    List list = getHibernateTemplate().find("select ab from ItemData ab, QuestionPoolItemData qpi where ab.itemId=qpi.itemId and qpi.questionPoolId = ?",
0429:                //                                            new Object[] {questionPoolId}
0430:                //                                            ,
0431:                //                                            new org.hibernate.type.Type[] {Hibernate.
0432:                //                                            LONG});
0433:                ArrayList itemList = new ArrayList();
0434:                for (int i = 0; i < list.size(); i++) {
0435:                    ItemData itemdata = (ItemData) list.get(i);
0436:                    ItemFacade f = new ItemFacade(itemdata);
0437:                    itemList.add(f);
0438:                }
0439:                return itemList;
0440:
0441:            }
0442:
0443:            private void populateQuestionPoolItemDatas(QuestionPoolData qpp) {
0444:                try {
0445:                    Set questionPoolItems = qpp.getQuestionPoolItems();
0446:                    if (questionPoolItems != null) {
0447:
0448:                        // let's get all the items for the specified pool in one shot
0449:                        HashMap h = new HashMap();
0450:                        List itemList = getAllItems(qpp.getQuestionPoolId());
0451:
0452:                        Iterator j = itemList.iterator();
0453:                        while (j.hasNext()) {
0454:                            ItemData itemData = (ItemData) j.next();
0455:                            h.put(itemData.getItemIdString(), itemData);
0456:                        }
0457:                        ArrayList itemArrayList = new ArrayList();
0458:                        Iterator i = questionPoolItems.iterator();
0459:                        while (i.hasNext()) {
0460:                            QuestionPoolItemData questionPoolItem = (QuestionPoolItemData) i
0461:                                    .next();
0462:                            ItemData itemData_0 = (ItemData) h
0463:                                    .get(questionPoolItem.getItemId());
0464:                            /*
0465:                            Set itemTextSet = itemData_0.getItemTextSet();
0466:                            Iterator k = itemTextSet.iterator();
0467:                            while (k.hasNext()) {
0468:                              ItemText itemText = (ItemText) k.next();
0469:                            }
0470:                             */
0471:                            itemArrayList.add(itemData_0);
0472:                        }
0473:                        qpp.setQuestions(itemArrayList);
0474:                        qpp.setSubPoolSize(new Integer(getSubPoolSize(qpp
0475:                                .getQuestionPoolId())));
0476:                    }
0477:                } catch (Exception e) {
0478:                    e.printStackTrace();
0479:                }
0480:            }
0481:
0482:            /**
0483:             * This method returns an ItemFacade that we can use to construct our ItemImpl
0484:             */
0485:            public ItemFacade getItem(String id) {
0486:                ItemData item = (ItemData) getHibernateTemplate().load(
0487:                        ItemData.class, id);
0488:                return new ItemFacade(item);
0489:            }
0490:
0491:            /**
0492:             * Get a pool based on poolId. I am not sure why agent is not used though is being parsed.
0493:             *
0494:             * @param poolid DOCUMENTATION PENDING
0495:             * @param agent DOCUMENTATION PENDING
0496:             *
0497:             * @return DOCUMENTATION PENDING
0498:             */
0499:            public QuestionPoolFacade getPool(Long poolId, String agent) {
0500:                try {
0501:                    QuestionPoolData qpp = (QuestionPoolData) getHibernateTemplate()
0502:                            .load(QuestionPoolData.class, poolId);
0503:                    // setAccessType
0504:                    setPoolAccessType(qpp, agent);
0505:                    // QuestionPoolItemData's identifier is a compsite identifier made up of
0506:                    // poolId and itemId <-- is regarded as "legacy DB" in Hibernate language.
0507:                    // We need to construct the properties for such as object ourselves.
0508:                    populateQuestionPoolItemDatas(qpp);
0509:                    return getQuestionPool(qpp);
0510:                } catch (Exception e) {
0511:                    log.error(e);
0512:                    return null;
0513:                }
0514:            }
0515:
0516:            public void setPoolAccessType(QuestionPoolData qpp, String agentId) {
0517:                try {
0518:                    QuestionPoolAccessData qpa = getQuestionPoolAccessData(qpp
0519:                            .getQuestionPoolId(), agentId);
0520:                    if (qpa == null) {
0521:                        // if (qpa == null), take what is set for pool.
0522:                    } else {
0523:                        qpp.setAccessTypeId(qpa.getAccessTypeId());
0524:                    }
0525:                } catch (Exception e) {
0526:                    log.warn(e.getMessage());
0527:                }
0528:            }
0529:
0530:            public QuestionPoolAccessData getQuestionPoolAccessData(
0531:                    final Long poolId, final String agentId) {
0532:                final HibernateCallback hcb = new HibernateCallback() {
0533:                    public Object doInHibernate(Session session)
0534:                            throws HibernateException, SQLException {
0535:                        Query q = session
0536:                                .createQuery("from QuestionPoolAccessData as qpa where qpa.questionPoolId =? and qpa.agentId=?");
0537:                        q.setLong(0, poolId.longValue());
0538:                        q.setString(1, agentId);
0539:                        return q.list();
0540:                    };
0541:                };
0542:                List list = getHibernateTemplate().executeFind(hcb);
0543:
0544:                //    List list = getHibernateTemplate().find("from QuestionPoolAccessData as qpa where qpa.questionPoolId =? and qpa.agentId=?",
0545:                //                                            new Object[] {poolId, agentId}
0546:                //                                            ,
0547:                //                                            new org.hibernate.type.Type[] {Hibernate.
0548:                //                                            LONG, Hibernate.STRING});
0549:                return (QuestionPoolAccessData) list.get(0);
0550:            }
0551:
0552:            /**
0553:             * DOCUMENTATION PENDING
0554:             *
0555:             * @param ids DOCUMENTATION PENDING
0556:             * @param sectionId DOCUMENTATION PENDING
0557:             */
0558:            public void addItemsToSection(Collection ids, long sectionId) {
0559:
0560:            }
0561:
0562:            /**
0563:             * add a question to a pool
0564:             *
0565:             * @param itemId DOCUMENTATION PENDING
0566:             * @param poolId DOCUMENTATION PENDING
0567:             */
0568:            public void addItemToPool(QuestionPoolItemData qpi) {
0569:                int retryCount = PersistenceService.getInstance()
0570:                        .getRetryCount().intValue();
0571:                while (retryCount > 0) {
0572:                    try {
0573:                        getHibernateTemplate().save(qpi);
0574:                        retryCount = 0;
0575:                    } catch (Exception e) {
0576:                        log.warn("problem saving item to pool: "
0577:                                + e.getMessage());
0578:                        retryCount = PersistenceService.getInstance()
0579:                                .retryDeadlock(e, retryCount);
0580:                    }
0581:                }
0582:
0583:            }
0584:
0585:            /**
0586:             * Delete pool and questions attached to it plus any subpool under it
0587:             *
0588:             * @param itemId DOCUMENTATION PENDING
0589:             * @param poolId DOCUMENTATION PENDING
0590:             */
0591:            public void deletePool(final Long poolId, String agent, Tree tree) {
0592:                try {
0593:                    QuestionPoolData questionPool = (QuestionPoolData) getHibernateTemplate()
0594:                            .load(QuestionPoolData.class, poolId);
0595:
0596:                    // #1. delete all questions which mean AssetBeanie (not ItemImpl) 'cos AssetBeanie
0597:                    // is the one that is associated with the DB
0598:                    // lydial:  getting list of items that only belong to this pool and not linked to any assessments. 
0599:                    List itemList = getAllItemsInThisPoolOnlyAndDetachFromAssessment(poolId);
0600:
0601:                    int retryCount = PersistenceService.getInstance()
0602:                            .getRetryCount().intValue();
0603:                    while (retryCount > 0) {
0604:                        try {
0605:                            getHibernateTemplate().deleteAll(itemList); // delete all AssetBeanie
0606:                            retryCount = 0;
0607:                        } catch (DataAccessException e) {
0608:                            log.warn("problem delete all items in pool: "
0609:                                    + e.getMessage());
0610:                            retryCount = PersistenceService.getInstance()
0611:                                    .retryDeadlock(e, retryCount);
0612:                        }
0613:                    }
0614:
0615:                    // #2. delete question and questionpool map.
0616:                    retryCount = PersistenceService.getInstance()
0617:                            .getRetryCount().intValue();
0618:                    while (retryCount > 0) {
0619:                        try {
0620:                            final HibernateCallback hcb = new HibernateCallback() {
0621:                                public Object doInHibernate(Session session)
0622:                                        throws HibernateException, SQLException {
0623:                                    Query q = session
0624:                                            .createQuery("select qpi from QuestionPoolItemData as qpi where qpi.questionPoolId= ?");
0625:                                    q.setLong(0, poolId.longValue());
0626:                                    return q.list();
0627:                                };
0628:                            };
0629:                            List list = getHibernateTemplate().executeFind(hcb);
0630:
0631:                            // a. delete item and pool association in SAM_ITEMMETADATA_T - this is the primary
0632:                            // pool that item is attached to
0633:                            ArrayList metaList = new ArrayList();
0634:                            for (int j = 0; j < list.size(); j++) {
0635:                                String itemId = ((QuestionPoolItemData) list
0636:                                        .get(j)).getItemId();
0637:                                String query = "from ItemMetaData as meta where meta.item.itemId="
0638:                                        + itemId
0639:                                        + " and meta.label='"
0640:                                        + ItemMetaDataIfc.POOLID + "'";
0641:                                List m = getHibernateTemplate().find(query);
0642:                                if (m.size() > 0) {
0643:                                    ItemMetaDataIfc meta = (ItemMetaDataIfc) m
0644:                                            .get(0);
0645:                                    meta.setEntry(null);
0646:                                }
0647:                            }
0648:                            try {
0649:                                getHibernateTemplate()
0650:                                        .saveOrUpdateAll(metaList);
0651:                                retryCount = 0;
0652:                            } catch (DataAccessException e) {
0653:                                log
0654:                                        .warn("problem delete question and questionpool map inside itemMetaData: "
0655:                                                + e.getMessage());
0656:                                retryCount = PersistenceService.getInstance()
0657:                                        .retryDeadlock(e, retryCount);
0658:                            }
0659:
0660:                            // b. delete item and pool association in SAM_QUESTIONPOOLITEM_T
0661:                            if (list.size() > 0) {
0662:                                questionPool
0663:                                        .setQuestionPoolItems(new HashSet());
0664:                                getHibernateTemplate().deleteAll(list);
0665:                                retryCount = 0;
0666:                            } else
0667:                                retryCount = 0;
0668:                        } catch (DataAccessException e) {
0669:                            log
0670:                                    .warn("problem delete question and questionpool map: "
0671:                                            + e.getMessage());
0672:                            retryCount = PersistenceService.getInstance()
0673:                                    .retryDeadlock(e, retryCount);
0674:                        }
0675:                    }
0676:
0677:                    // #3. Pool is owned by one but can be shared by multiple agents. So need to
0678:                    // delete all QuestionPoolAccessData record first. This seems to be missing in Navigo, nope? - daisyf
0679:                    // Actually, I don't think we have ever implemented sharing between agents. So we may wnat to
0680:                    // clean up this bit of code - daisyf 07/07/06
0681:                    final HibernateCallback hcb = new HibernateCallback() {
0682:                        public Object doInHibernate(Session session)
0683:                                throws HibernateException, SQLException {
0684:                            Query q = session
0685:                                    .createQuery("select qpa from QuestionPoolAccessData as qpa where qpa.questionPoolId= ?");
0686:                            q.setLong(0, poolId.longValue());
0687:                            return q.list();
0688:                        };
0689:                    };
0690:                    List qpaList = getHibernateTemplate().executeFind(hcb);
0691:                    retryCount = PersistenceService.getInstance()
0692:                            .getRetryCount().intValue();
0693:                    while (retryCount > 0) {
0694:                        try {
0695:                            getHibernateTemplate().deleteAll(qpaList);
0696:                            retryCount = 0;
0697:                        } catch (DataAccessException e) {
0698:                            log
0699:                                    .warn("problem delete question pool access data: "
0700:                                            + e.getMessage());
0701:                            retryCount = PersistenceService.getInstance()
0702:                                    .retryDeadlock(e, retryCount);
0703:                        }
0704:                    }
0705:
0706:                    // #4. Ready! delete pool now
0707:                    final HibernateCallback hcb2 = new HibernateCallback() {
0708:                        public Object doInHibernate(Session session)
0709:                                throws HibernateException, SQLException {
0710:                            Query q = session
0711:                                    .createQuery("select qp from QuestionPoolData as qp where qp.id= ?");
0712:                            q.setLong(0, poolId.longValue());
0713:                            return q.list();
0714:                        };
0715:                    };
0716:                    List qppList = getHibernateTemplate().executeFind(hcb2);
0717:                    retryCount = PersistenceService.getInstance()
0718:                            .getRetryCount().intValue();
0719:                    while (retryCount > 0) {
0720:                        try {
0721:                            getHibernateTemplate().deleteAll(qppList);
0722:                            retryCount = 0;
0723:                        } catch (DataAccessException e) {
0724:                            log.warn("problem delete all pools: "
0725:                                    + e.getMessage());
0726:                            retryCount = PersistenceService.getInstance()
0727:                                    .retryDeadlock(e, retryCount);
0728:                        }
0729:                    }
0730:
0731:                    // #5. delete all subpools if any, this is recursive
0732:                    Iterator citer = (tree.getChildList(poolId)).iterator();
0733:                    while (citer.hasNext()) {
0734:                        deletePool((Long) citer.next(), agent, tree);
0735:                    }
0736:                } catch (DataAccessException e) {
0737:                    log.warn("error deleting pool. " + e.getMessage());
0738:                }
0739:            }
0740:
0741:            /**
0742:             * Move pool under another pool. The dest pool must not be the
0743:             * descendant of the source nor can they be the same pool .
0744:             */
0745:            public void movePool(String agentId, Long sourcePoolId,
0746:                    Long destPoolId) {
0747:                try {
0748:                    QuestionPoolFacade sourcePool = getPool(sourcePoolId,
0749:                            agentId);
0750:                    if (destPoolId.equals(QuestionPoolFacade.ROOT_POOL)
0751:                            && !sourcePoolId
0752:                                    .equals(QuestionPoolFacade.ROOT_POOL)) {
0753:                        sourcePool
0754:                                .setParentPoolId(QuestionPoolFacade.ROOT_POOL);
0755:                        int retryCount = PersistenceService.getInstance()
0756:                                .getRetryCount().intValue();
0757:                        while (retryCount > 0) {
0758:                            try {
0759:                                getHibernateTemplate()
0760:                                        .update(
0761:                                                (QuestionPoolData) sourcePool
0762:                                                        .getData());
0763:                                retryCount = 0;
0764:                            } catch (DataAccessException e) {
0765:                                log.warn("problem moving pool: "
0766:                                        + e.getMessage());
0767:                                retryCount = PersistenceService.getInstance()
0768:                                        .retryDeadlock(e, retryCount);
0769:                            }
0770:                        }
0771:                    } else {
0772:                        QuestionPoolFacade destPool = getPool(destPoolId,
0773:                                agentId);
0774:                        sourcePool
0775:                                .setParentPoolId(destPool.getQuestionPoolId());
0776:                        int retryCount = PersistenceService.getInstance()
0777:                                .getRetryCount().intValue();
0778:                        while (retryCount > 0) {
0779:                            try {
0780:                                getHibernateTemplate()
0781:                                        .update(
0782:                                                (QuestionPoolData) sourcePool
0783:                                                        .getData());
0784:                                retryCount = 0;
0785:                            } catch (DataAccessException e) {
0786:                                log.warn("problem update source pool: "
0787:                                        + e.getMessage());
0788:                                retryCount = PersistenceService.getInstance()
0789:                                        .retryDeadlock(e, retryCount);
0790:                            }
0791:                        }
0792:                    }
0793:                } catch (RuntimeException e) {
0794:                    log.warn(e.getMessage());
0795:                }
0796:            }
0797:
0798:            /**
0799:             * Is destination a descendant of the source?
0800:             */
0801:            public boolean isDescendantOf(QuestionPoolFacade destPool,
0802:                    QuestionPoolFacade sourcePool) {
0803:
0804:                Long tempPoolId = destPool.getQuestionPoolId();
0805:                try {
0806:                    while ((tempPoolId != null)
0807:                            && (!tempPoolId
0808:                                    .equals(QuestionPoolFacade.ROOT_POOL))) {
0809:                        QuestionPoolFacade tempPool = getPoolById(tempPoolId);
0810:                        if (tempPool.getParentPoolId().equals(
0811:                                sourcePool.getQuestionPoolId())) {
0812:                            return true;
0813:                        }
0814:                        tempPoolId = tempPool.getParentPoolId();
0815:                    }
0816:                    return false;
0817:                } catch (Exception e) {
0818:                    log.warn(e.getMessage());
0819:                    return false;
0820:                }
0821:            }
0822:
0823:            /**
0824:             * DOCUMENTATION PENDING
0825:             *
0826:             * @param itemId DOCUMENTATION PENDING
0827:             * @param poolId DOCUMENTATION PENDING
0828:             */
0829:            public void removeItemFromPool(String itemId, Long poolId) {
0830:                QuestionPoolItemData qpi = new QuestionPoolItemData(poolId,
0831:                        itemId);
0832:                int retryCount = PersistenceService.getInstance()
0833:                        .getRetryCount().intValue();
0834:                while (retryCount > 0) {
0835:                    try {
0836:                        getHibernateTemplate().delete(qpi);
0837:                        retryCount = 0;
0838:                    } catch (Exception e) {
0839:                        log.warn("problem delete item from pool: "
0840:                                + e.getMessage());
0841:                        retryCount = PersistenceService.getInstance()
0842:                                .retryDeadlock(e, retryCount);
0843:                    }
0844:                }
0845:            }
0846:
0847:            /**
0848:             * DOCUMENTATION PENDING
0849:             *
0850:             * @param itemId DOCUMENTATION PENDING
0851:             * @param poolId DOCUMENTATION PENDING
0852:             */
0853:            public void moveItemToPool(String itemId, Long sourceId, Long destId) {
0854:                QuestionPoolItemData qpi = new QuestionPoolItemData(sourceId,
0855:                        itemId);
0856:                int retryCount = PersistenceService.getInstance()
0857:                        .getRetryCount().intValue();
0858:                while (retryCount > 0) {
0859:                    try {
0860:                        getHibernateTemplate().delete(qpi);
0861:                        retryCount = 0;
0862:                    } catch (Exception e) {
0863:                        log.warn("problem delete old mapping: "
0864:                                + e.getMessage());
0865:                        retryCount = PersistenceService.getInstance()
0866:                                .retryDeadlock(e, retryCount);
0867:                    }
0868:                }
0869:                QuestionPoolItemData qpi2 = new QuestionPoolItemData(destId,
0870:                        itemId);
0871:                retryCount = PersistenceService.getInstance().getRetryCount()
0872:                        .intValue();
0873:                while (retryCount > 0) {
0874:                    try {
0875:                        getHibernateTemplate().save(qpi2);
0876:                        retryCount = 0;
0877:                    } catch (Exception e) {
0878:                        log.warn("problem saving new mapping: "
0879:                                + e.getMessage());
0880:                        retryCount = PersistenceService.getInstance()
0881:                                .retryDeadlock(e, retryCount);
0882:                    }
0883:                }
0884:            }
0885:
0886:            /**
0887:             * DOCUMENTATION PENDING
0888:             *
0889:             * @param pool DOCUMENTATION PENDING
0890:             */
0891:            public QuestionPoolFacade savePool(QuestionPoolFacade pool) {
0892:                boolean insert = false;
0893:                try {
0894:                    QuestionPoolData qpp = (QuestionPoolData) pool.getData();
0895:                    qpp.setLastModified(new Date());
0896:                    qpp.setLastModifiedById(AgentFacade.getAgentString());
0897:                    int retryCount = PersistenceService.getInstance()
0898:                            .getRetryCount().intValue();
0899:                    if (qpp.getQuestionPoolId() == null
0900:                            || qpp.getQuestionPoolId().equals(new Long("0"))) { // indicate a new pool
0901:                        insert = true;
0902:                    }
0903:                    while (retryCount > 0) {
0904:                        try {
0905:                            getHibernateTemplate().saveOrUpdate(qpp);
0906:                            retryCount = 0;
0907:                        } catch (DataAccessException e) {
0908:                            log.warn("problem saving Or Update pool: "
0909:                                    + e.getMessage());
0910:                            retryCount = PersistenceService.getInstance()
0911:                                    .retryDeadlock(e, retryCount);
0912:                        }
0913:                    }
0914:
0915:                    if (insert) {
0916:                        // add a QuestionPoolAccessData record for the owner who should have ADMIN access to the pool
0917:                        QuestionPoolAccessData qpa = new QuestionPoolAccessData(
0918:                                qpp.getQuestionPoolId(), qpp.getOwnerId(),
0919:                                QuestionPoolData.ADMIN);
0920:                        retryCount = PersistenceService.getInstance()
0921:                                .getRetryCount().intValue();
0922:                        while (retryCount > 0) {
0923:                            try {
0924:                                getHibernateTemplate().save(qpa);
0925:                                retryCount = 0;
0926:                            } catch (DataAccessException e) {
0927:                                log.warn("problem saving pool: "
0928:                                        + e.getMessage());
0929:                                retryCount = PersistenceService.getInstance()
0930:                                        .retryDeadlock(e, retryCount);
0931:                            }
0932:                        }
0933:                    }
0934:                    return pool;
0935:                } catch (RuntimeException e) {
0936:                    log.warn(e.getMessage());
0937:                    return null;
0938:                }
0939:            }
0940:
0941:            /**
0942:             * Get all the children pools of a pool. Return a list of QuestionPoolData
0943:             * should return QuestionPool instead - need fixing, daisyf
0944:             *
0945:             * @param itemId DOCUMENTATION PENDING
0946:             * @param poolId DOCUMENTATION PENDING
0947:             */
0948:
0949:            public List getSubPools(final Long poolId) {
0950:                final HibernateCallback hcb = new HibernateCallback() {
0951:                    public Object doInHibernate(Session session)
0952:                            throws HibernateException, SQLException {
0953:                        Query q = session
0954:                                .createQuery("from QuestionPoolData as qpp where qpp.parentPoolId=?");
0955:                        q.setLong(0, poolId.longValue());
0956:                        return q.list();
0957:                    };
0958:                };
0959:                return getHibernateTemplate().executeFind(hcb);
0960:            }
0961:
0962:            public int getSubPoolSize(Long poolId) {
0963:                return getSubPools(poolId).size();
0964:            }
0965:
0966:            /**
0967:             * DOCUMENTATION PENDING
0968:             *
0969:             * @param itemId DOCUMENTATION PENDING
0970:             * @param poolId DOCUMENTATION PENDING
0971:             */
0972:
0973:            public boolean hasSubPools(final Long poolId) {
0974:                final HibernateCallback hcb = new HibernateCallback() {
0975:                    public Object doInHibernate(Session session)
0976:                            throws HibernateException, SQLException {
0977:                        Query q = session
0978:                                .createQuery("from QuestionPoolData as qpp where qpp.parentPoolId=?");
0979:                        q.setLong(0, poolId.longValue());
0980:                        return q.list();
0981:                    };
0982:                };
0983:                List subPools = getHibernateTemplate().executeFind(hcb);
0984:
0985:                //    List subPools =
0986:                //        getHibernateTemplate().find(
0987:                //        "from QuestionPoolData as qpp where qpp.parentPoolId=?",
0988:                //        new Object[] {poolId}
0989:                //        , new org.hibernate.type.Type[] {Hibernate.LONG});
0990:                if (subPools.size() > 0) {
0991:                    return true;
0992:                } else {
0993:                    return false;
0994:                }
0995:            }
0996:
0997:            public boolean poolIsUnique(final Long questionPoolId,
0998:                    final String title, final Long parentPoolId,
0999:                    final String agentId) {
1000:                final HibernateCallback hcb = new HibernateCallback() {
1001:                    public Object doInHibernate(Session session)
1002:                            throws HibernateException, SQLException {
1003:                        Query q = session
1004:                                .createQuery("select new QuestionPoolData(a.questionPoolId, a.title, a.parentPoolId)from QuestionPoolData a where a.questionPoolId!= ? and a.title=? and a.parentPoolId=? and a.ownerId = ? ");
1005:                        q.setLong(0, questionPoolId.longValue());
1006:                        q.setString(1, title);
1007:                        q.setLong(2, parentPoolId.longValue());
1008:                        q.setString(3, agentId);
1009:                        return q.list();
1010:                    };
1011:                };
1012:                List list = getHibernateTemplate().executeFind(hcb);
1013:
1014:                //     List list = getHibernateTemplate().find(
1015:                //        "select new QuestionPoolData(a.questionPoolId, a.title, a.parentPoolId)from QuestionPoolData a where a.questionPoolId!= ? and a.title=? and a.parentPoolId=?",
1016:                //        new Object[] {questionPoolId,title,parentPoolId}
1017:                //       , new org.hibernate.type.Type[] {Hibernate.LONG,Hibernate.STRING, Hibernate.LONG});
1018:                boolean isUnique = true;
1019:                if (list.size() > 0) {
1020:                    // query in mysql & hsqldb are not case sensitive, check that title found is indeed what we
1021:                    // are looking (SAK-3110)
1022:                    for (int i = 0; i < list.size(); i++) {
1023:                        QuestionPoolData q = (QuestionPoolData) list.get(i);
1024:                        if ((title).equals(q.getTitle().trim())) {
1025:                            isUnique = false;
1026:                            break;
1027:                        }
1028:                    }
1029:                }
1030:                return isUnique;
1031:            }
1032:
1033:            /**
1034:             * Return a list of questionPoolId (java.lang.Long)
1035:             *
1036:             * @param itemId DOCUMENTATION PENDING
1037:             * @param poolId DOCUMENTATION PENDING
1038:             */
1039:
1040:            public List getPoolIdsByAgent(final String agentId) {
1041:                ArrayList idList = new ArrayList();
1042:
1043:                final HibernateCallback hcb = new HibernateCallback() {
1044:                    public Object doInHibernate(Session session)
1045:                            throws HibernateException, SQLException {
1046:                        Query q = session
1047:                                .createQuery("select qpa from QuestionPoolAccessData as qpa where qpa.agentId= ?");
1048:                        q.setString(0, agentId);
1049:                        return q.list();
1050:                    };
1051:                };
1052:                List qpaList = getHibernateTemplate().executeFind(hcb);
1053:
1054:                //    List qpaList = getHibernateTemplate().find(
1055:                //        "select qpa from QuestionPoolAccessData as qpa where qpa.agentId= ?",
1056:                //        new Object[] {agentId}
1057:                //        , new org.hibernate.type.Type[] {Hibernate.STRING});
1058:                try {
1059:                    Iterator iter = qpaList.iterator();
1060:                    while (iter.hasNext()) {
1061:                        QuestionPoolAccessData qpa = (QuestionPoolAccessData) iter
1062:                                .next();
1063:                        idList.add(qpa.getQuestionPoolId()); // return a list of poolId (java.lang.Long)
1064:                    }
1065:                    return idList;
1066:                } catch (RuntimeException e) {
1067:                    return null;
1068:                }
1069:            }
1070:
1071:            /**
1072:             * Return a list of questionPoolId (java.lang.Long)
1073:             *
1074:             * @param itemId DOCUMENTATION PENDING
1075:             * @param poolId DOCUMENTATION PENDING
1076:             */
1077:
1078:            public List getPoolIdsByItem(final String itemId) {
1079:                ArrayList idList = new ArrayList();
1080:
1081:                final HibernateCallback hcb = new HibernateCallback() {
1082:                    public Object doInHibernate(Session session)
1083:                            throws HibernateException, SQLException {
1084:                        Query q = session
1085:                                .createQuery("select qpi from QuestionPoolItemData as qpi where qpi.itemId= ?");
1086:                        q.setString(0, itemId);
1087:                        return q.list();
1088:                    };
1089:                };
1090:                List qpiList = getHibernateTemplate().executeFind(hcb);
1091:
1092:                //    List qpiList = getHibernateTemplate().find(
1093:                //        "select qpi from QuestionPoolItemData as qpi where qpi.itemId= ?",
1094:                //        new Object[] {itemId}
1095:                //        , new org.hibernate.type.Type[] {Hibernate.STRING});
1096:                try {
1097:                    Iterator iter = qpiList.iterator();
1098:                    while (iter.hasNext()) {
1099:                        QuestionPoolItemData qpa = (QuestionPoolItemData) iter
1100:                                .next();
1101:                        idList.add(qpa.getQuestionPoolId()); // return a list of poolId (java.lang.Long)
1102:                    }
1103:                    return idList;
1104:                } catch (Exception e) {
1105:                    e.printStackTrace();
1106:                    return null;
1107:                }
1108:            }
1109:
1110:            /**
1111:             * Copy a pool to a new location.
1112:             */
1113:            public void copyPool(Tree tree, String agentId, Long sourceId,
1114:                    Long destId, String prependString1, String prependString2) {
1115:                try {
1116:                    boolean haveCommonRoot = false;
1117:                    boolean duplicate = false;
1118:
1119:                    // Get the Pools
1120:                    QuestionPoolFacade oldPool = getPool(sourceId, agentId);
1121:                    String oldPoolName = oldPool.getDisplayName();
1122:
1123:                    // Are we creating a duplicate under the same parent?
1124:                    if (destId.equals(oldPool.getParentPoolId())) {
1125:                        duplicate = true;
1126:                    }
1127:
1128:                    // Determine if the Pools are in the same tree
1129:                    // If so, make sure the source level is not higher(up the tree)
1130:                    // than the dest. to avoid the endless loop.
1131:                    if (!duplicate) {
1132:                        haveCommonRoot = tree.haveCommonRoot(sourceId, destId);
1133:                    }
1134:
1135:                    if (haveCommonRoot
1136:                            && (tree.poolLevel(sourceId) <= tree
1137:                                    .poolLevel(destId))) {
1138:                        return; // Since otherwise it would cause an infinite loop.
1139:                        // We should revisit this.
1140:                    }
1141:
1142:                    QuestionPoolFacade newPool = (QuestionPoolFacade) oldPool
1143:                            .clone();
1144:                    newPool.setParentPoolId(destId);
1145:                    newPool.setQuestionPoolId(new Long(0));
1146:
1147:                    // If Pools in same trees,
1148:                    if (!haveCommonRoot) {
1149:                        // If Pools in different trees,
1150:                        // Copy to a Pool outside the same root
1151:                        // Copy *this* Pool first
1152:                        if (duplicate)
1153:                            resetTitle(destId, newPool, oldPoolName,
1154:                                    prependString1, prependString2);
1155:                        else
1156:                            newPool.updateDisplayName(oldPoolName);
1157:                    }
1158:
1159:                    newPool = savePool(newPool);
1160:                    Iterator iter = oldPool.getQuestions().iterator();
1161:                    ArrayList itemDataArray = new ArrayList();
1162:                    while (iter.hasNext()) {
1163:                        ItemDataIfc itemData = (ItemDataIfc) iter.next();
1164:                        ItemFacade itemFacade = copyItemFacade2(itemData);
1165:                        ItemDataIfc newItemData = itemFacade.getData();
1166:                        itemDataArray.add(newItemData);
1167:                    }
1168:
1169:                    // then save question to pool
1170:                    newPool.setQuestionPoolItems(prepareQuestions(newPool
1171:                            .getQuestionPoolId(), itemDataArray));
1172:                    newPool.setQuestions(itemDataArray);
1173:                    newPool = savePool(newPool);
1174:
1175:                    // Get the SubPools of oldPool
1176:                    Iterator citer = (tree.getChildList(sourceId)).iterator();
1177:                    while (citer.hasNext()) {
1178:                        Long childPoolId = (Long) citer.next();
1179:                        copyPool(tree, agentId, childPoolId, newPool
1180:                                .getQuestionPoolId(), prependString1,
1181:                                prependString2);
1182:                    }
1183:                } catch (Exception e) {
1184:                    e.printStackTrace();
1185:                }
1186:            }
1187:
1188:            /*
1189:            public static void main(String[] args) throws DataFacadeException {
1190:              QuestionPoolFacadeQueriesAPI instance = new QuestionPoolFacadeQueries();
1191:              // add an item
1192:              if (args[0].equals("add")) {
1193:                Long questionPoolId = instance.add();
1194:              }
1195:              if (args[0].equals("getQPItems")) {
1196:                List items = instance.getAllItems(new Long(args[1])); // poolId
1197:                for (int i = 0; i < items.size(); i++) {
1198:                  ItemData item = (ItemData) items.get(i);
1199:                }
1200:              }
1201:              System.exit(0);
1202:            }
1203:             */
1204:
1205:            public Long add() {
1206:                QuestionPoolData questionPool = new QuestionPoolData();
1207:                questionPool.setTitle("Daisy Happy Pool");
1208:                questionPool.setOwnerId("1");
1209:                questionPool.setDateCreated(new Date());
1210:                questionPool.setLastModifiedById("1");
1211:                questionPool.setLastModified(new Date());
1212:                getHibernateTemplate().save(questionPool);
1213:                return questionPool.getQuestionPoolId();
1214:            }
1215:
1216:            public QuestionPoolFacade getPoolById(Long questionPoolId) {
1217:                QuestionPoolFacade questionPoolFacade = null;
1218:                try {
1219:                    if (!questionPoolId.equals(QuestionPoolFacade.ROOT_POOL)) {
1220:                        QuestionPoolData questionPool = (QuestionPoolData) getHibernateTemplate()
1221:                                .load(QuestionPoolData.class, questionPoolId);
1222:                        if (questionPool != null) {
1223:                            questionPoolFacade = new QuestionPoolFacade(
1224:                                    questionPool);
1225:                        }
1226:                    }
1227:                } catch (Exception e) {
1228:                    e.printStackTrace();
1229:                }
1230:                return questionPoolFacade;
1231:            }
1232:
1233:            public HashMap getQuestionPoolItemMap() {
1234:                HashMap h = new HashMap();
1235:                String query = "from QuestionPoolItemData";
1236:                List l = getHibernateTemplate().find(query);
1237:                for (int i = 0; i < l.size(); i++) {
1238:                    QuestionPoolItemData q = (QuestionPoolItemData) l.get(i);
1239:                    h.put(q.getItemId(), q);
1240:                }
1241:                return h;
1242:            }
1243:
1244:            public HashSet prepareQuestions(Long questionPoolId,
1245:                    ArrayList itemDataArray) {
1246:                HashSet set = new HashSet();
1247:                Iterator iter = itemDataArray.iterator();
1248:                while (iter.hasNext()) {
1249:                    ItemDataIfc itemData = (ItemDataIfc) iter.next();
1250:                    set.add(new QuestionPoolItemData(questionPoolId, itemData
1251:                            .getItemIdString(), (ItemData) itemData));
1252:                }
1253:                return set;
1254:            }
1255:
1256:            /*
1257:            public HashSet prepareQuestions(Long questionPoolId, Set questionSet){
1258:              HashSet set = new HashSet();
1259:              Iterator iter = questionSet.iterator();
1260:              while (iter.hasNext()){
1261:                QuestionPoolItemData i = (QuestionPoolItemData)iter.next();
1262:                set.add(new QuestionPoolItemData(questionPoolId, i.getItemId()));
1263:              }
1264:              return set;
1265:            }
1266:             */
1267:
1268:            private void resetTitle(Long destId, QuestionPoolFacade newPool,
1269:                    String oldPoolName, String prependString1,
1270:                    String prependString2) {
1271:                //find name by loop through sibslings
1272:                List siblings = getSubPools(destId);
1273:                int num = 0;
1274:                int startIndex = 0;
1275:                int endIndex = 0;
1276:                int maxNum = 0;
1277:                StringBuffer prependString = new StringBuffer(prependString1);
1278:                prependString.append(" ");
1279:                prependString.append(prependString2);
1280:                prependString.append(" ");
1281:                for (int l = 0; l < siblings.size(); l++) {
1282:                    QuestionPoolData a = (QuestionPoolData) siblings.get(l);
1283:                    String n = a.getTitle();
1284:                    if (n.startsWith(prependString.toString())) {
1285:                        if (n.equals(prependString + oldPoolName)) {
1286:                            if (maxNum < 1)
1287:                                maxNum = 1;
1288:                        }
1289:                    }
1290:                    if (n.startsWith(prependString1 + "(")) {
1291:                        startIndex = n.indexOf("(");
1292:                        endIndex = n.indexOf(")");
1293:                        try {
1294:                            String partialPoolName = n.substring(endIndex + 2)
1295:                                    .replaceFirst(prependString2 + " ", "")
1296:                                    .trim();
1297:                            num = Integer.parseInt(n.substring(startIndex + 1,
1298:                                    endIndex));
1299:                            if (oldPoolName.equals(partialPoolName)) {
1300:                                if (num > maxNum)
1301:                                    maxNum = num;
1302:                            }
1303:                        } catch (NumberFormatException e) {
1304:                            log.warn("rename title of duplicate pool:"
1305:                                    + e.getMessage());
1306:                        }
1307:                    }
1308:                }
1309:
1310:                if (maxNum == 0)
1311:                    newPool.updateDisplayName(prependString + oldPoolName);
1312:                else
1313:                    newPool.updateDisplayName(prependString1 + "("
1314:                            + (maxNum + 1) + ") " + prependString2 + " "
1315:                            + oldPoolName);
1316:            }
1317:
1318:            public Long copyItemFacade(ItemDataIfc itemData) {
1319:                ItemFacade item = getItemFacade(itemData);
1320:                ItemService itemService = new ItemService();
1321:                Long itemId = itemService.saveItem(item).getItemId();
1322:
1323:                return itemId;
1324:            }
1325:
1326:            public ItemFacade copyItemFacade2(ItemDataIfc itemData) {
1327:                ItemFacade item = getItemFacade(itemData);
1328:                ItemService itemService = new ItemService();
1329:                return itemService.saveItem(item);
1330:            }
1331:
1332:            private ItemFacade getItemFacade(ItemDataIfc itemData) {
1333:                ItemFacade item = new ItemFacade();
1334:                item.setScore(itemData.getScore());
1335:                item.setHint(itemData.getHint());
1336:                item.setStatus(itemData.getStatus());
1337:                item.setTypeId(itemData.getTypeId());
1338:                item.setCreatedBy(AgentFacade.getAgentString());
1339:                item.setCreatedDate(new Date());
1340:                item.setLastModifiedBy(AgentFacade.getAgentString());
1341:                item.setLastModifiedDate(new Date());
1342:                item.setInstruction(itemData.getInstruction());
1343:                item.setHasRationale(itemData.getHasRationale());
1344:                item.setTriesAllowed(itemData.getTriesAllowed());
1345:                item.setDuration(itemData.getDuration());
1346:
1347:                item.setItemTextSet(copyItemText(item.getData(), itemData));
1348:                item.setItemMetaDataSet(copyMetaData(item.getData(), itemData));
1349:                item.setItemAttachmentSet(copyAttachment(item.getData(),
1350:                        itemData));
1351:
1352:                if (itemData.getCorrectItemFeedback() != null
1353:                        && !itemData.getCorrectItemFeedback().equals("")) {
1354:                    item.setCorrectItemFeedback(itemData
1355:                            .getCorrectItemFeedback());
1356:                }
1357:                if (itemData.getInCorrectItemFeedback() != null
1358:                        && !itemData.getInCorrectItemFeedback().equals("")) {
1359:                    item.setInCorrectItemFeedback(itemData
1360:                            .getInCorrectItemFeedback());
1361:                }
1362:                if (itemData.getGeneralItemFeedback() != null
1363:                        && !itemData.getGeneralItemFeedback().equals("")) {
1364:                    item.setGeneralItemFeedback(itemData
1365:                            .getGeneralItemFeedback());
1366:                }
1367:
1368:                return item;
1369:            }
1370:
1371:            private HashSet copyItemText(ItemDataIfc toItemData,
1372:                    ItemDataIfc fromItemData) {
1373:                HashSet toItemTextSet = new HashSet();
1374:                Set fromItemTextSet = fromItemData.getItemTextSet();
1375:                Iterator itemTextIter = fromItemTextSet.iterator();
1376:                while (itemTextIter.hasNext()) {
1377:                    ItemText fromItemText = (ItemText) itemTextIter.next();
1378:                    ItemText toItemText = new ItemText();
1379:                    toItemText.setItem(toItemData);
1380:                    toItemText.setSequence(fromItemText.getSequence());
1381:                    toItemText.setText(fromItemText.getText());
1382:
1383:                    HashSet toAnswerSet = new HashSet();
1384:                    Set fromAnswerSet = fromItemText.getAnswerSet();
1385:                    Iterator answerIter = fromAnswerSet.iterator();
1386:                    while (answerIter.hasNext()) {
1387:                        Answer fromAnswer = (Answer) answerIter.next();
1388:                        Answer toAnswer = new Answer(toItemText, fromAnswer
1389:                                .getText(), fromAnswer.getSequence(),
1390:                                fromAnswer.getLabel(), fromAnswer
1391:                                        .getIsCorrect(), fromAnswer.getGrade(),
1392:                                fromAnswer.getScore());
1393:
1394:                        HashSet toAnswerFeedbackSet = new HashSet();
1395:                        Set fromAnswerFeedbackSet = fromAnswer
1396:                                .getAnswerFeedbackSet();
1397:                        Iterator answerFeedbackIter = fromAnswerFeedbackSet
1398:                                .iterator();
1399:                        while (answerFeedbackIter.hasNext()) {
1400:                            AnswerFeedback fromAnswerFeedback = (AnswerFeedback) answerFeedbackIter
1401:                                    .next();
1402:                            toAnswerFeedbackSet.add(new AnswerFeedback(
1403:                                    toAnswer, fromAnswerFeedback.getTypeId(),
1404:                                    fromAnswerFeedback.getText()));
1405:                            toAnswer.setAnswerFeedbackSet(toAnswerFeedbackSet);
1406:                        }
1407:                        toAnswerSet.add(toAnswer);
1408:                        toItemText.setAnswerSet(toAnswerSet);
1409:                    }
1410:                    toItemTextSet.add(toItemText);
1411:                }
1412:                return toItemTextSet;
1413:            }
1414:
1415:            private HashSet copyMetaData(ItemDataIfc toItemData,
1416:                    ItemDataIfc fromItemData) {
1417:                HashSet toSet = new HashSet();
1418:                Set fromSet = fromItemData.getItemMetaDataSet();
1419:                Iterator iter = fromSet.iterator();
1420:                while (iter.hasNext()) {
1421:                    ItemMetaData itemMetaData = (ItemMetaData) iter.next();
1422:                    toSet.add(new ItemMetaData(toItemData, itemMetaData
1423:                            .getLabel(), itemMetaData.getEntry()));
1424:                }
1425:                return toSet;
1426:            }
1427:
1428:            private HashSet copyAttachment(ItemDataIfc toItemData,
1429:                    ItemDataIfc fromItemData) {
1430:                HashSet toSet = new HashSet();
1431:                Set fromSet = fromItemData.getItemAttachmentSet();
1432:                Iterator iter = fromSet.iterator();
1433:                while (iter.hasNext()) {
1434:                    ItemAttachmentIfc fromItemAttachment = (ItemAttachmentIfc) iter
1435:                            .next();
1436:                    toSet.add(new ItemAttachment(fromItemAttachment
1437:                            .getAttachmentId(), toItemData, fromItemAttachment
1438:                            .getResourceId(), fromItemAttachment.getFilename(),
1439:                            fromItemAttachment.getMimeType(),
1440:                            fromItemAttachment.getFileSize(),
1441:                            fromItemAttachment.getDescription(),
1442:                            fromItemAttachment.getLocation(),
1443:                            fromItemAttachment.getIsLink(), fromItemAttachment
1444:                                    .getStatus(), AgentFacade.getAgentString(),
1445:                            new Date(), AgentFacade.getAgentString(),
1446:                            new Date()));
1447:                }
1448:                return toSet;
1449:            }
1450:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.