Source Code Cross Referenced for ItemFacadeQueries.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) 


001:        /**********************************************************************************
002:         * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/facade/ItemFacadeQueries.java $
003:         * $Id: ItemFacadeQueries.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004:         ***********************************************************************************
005:         *
006:         * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007:         *
008:         * Licensed under the Educational Community License, Version 1.0 (the"License");
009:         * you may not use this file except in compliance with the License.
010:         * You may obtain a copy of the License at
011:         *
012:         *      http://www.opensource.org/licenses/ecl1.php
013:         *
014:         * Unless required by applicable law or agreed to in writing, software
015:         * distributed under the License is distributed on an "AS IS" BASIS,
016:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017:         * See the License for the specific language governing permissions and
018:         * limitations under the License.
019:         *
020:         **********************************************************************************/package org.sakaiproject.tool.assessment.facade;
021:
022:        import java.sql.SQLException;
023:        import java.util.Date;
024:        import java.util.HashMap;
025:        import java.util.HashSet;
026:        import java.util.Set;
027:        import java.util.List;
028:        import java.util.Iterator;
029:
030:        import org.hibernate.HibernateException;
031:        import org.hibernate.Query;
032:        import org.hibernate.Session;
033:
034:        import org.apache.commons.logging.Log;
035:        import org.apache.commons.logging.LogFactory;
036:        import org.sakaiproject.tool.assessment.data.dao.assessment.Answer;
037:        import org.sakaiproject.tool.assessment.data.dao.assessment.AnswerFeedback;
038:        import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
039:        import org.sakaiproject.tool.assessment.data.dao.assessment.ItemMetaData;
040:        import org.sakaiproject.tool.assessment.data.dao.assessment.ItemText;
041:        import org.sakaiproject.tool.assessment.data.dao.shared.TypeD;
042:        import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
043:        import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
044:        import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemMetaDataIfc;
045:        import org.sakaiproject.tool.assessment.data.ifc.assessment.SectionDataIfc;
046:        import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl;
047:        import org.sakaiproject.tool.assessment.services.PersistenceService;
048:        import org.sakaiproject.tool.assessment.services.assessment.AssessmentService;
049:        import org.springframework.orm.hibernate3.HibernateCallback;
050:        import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
051:
052:        public class ItemFacadeQueries extends HibernateDaoSupport implements 
053:                ItemFacadeQueriesAPI {
054:            private static Log log = LogFactory.getLog(ItemFacadeQueries.class);
055:
056:            public ItemFacadeQueries() {
057:            }
058:
059:            public IdImpl getItemId(String id) {
060:                return new IdImpl(id);
061:            }
062:
063:            public IdImpl getItemId(Long id) {
064:                return new IdImpl(id);
065:            }
066:
067:            public IdImpl getItemId(long id) {
068:                return new IdImpl(id);
069:            }
070:
071:            /*
072:            public static void main(String[] args) throws DataFacadeException {
073:              ItemFacadeQueriesAPI instance = new ItemFacadeQueries();
074:              // add an item
075:              if (args[0].equals("add")) {
076:                Long itemId = instance.add();
077:                //log.debug("**Item #" + itemId);
078:                instance.show(itemId);
079:              }
080:              if (args[0].equals("f_add")) {
081:                Long itemId = new Long(-1);
082:                itemId = instance.facadeAdd();
083:                //log.debug("**Item #" + itemId);
084:                instance.ifcShow(itemId);
085:              }
086:              if (args[0].equals("show")) {
087:                instance.show(new Long(args[1]));
088:              }
089:              if (args[0].equals("showtype")) {
090:                instance.showType(new Long(args[1]));
091:              }
092:              if (args[0].equals("remove")) {
093:                instance.remove(new Long(args[1]));
094:              }
095:              if (args[0].equals("listtype")) {
096:                instance.listType();
097:              }
098:              if (args[0].equals("list")) {
099:                instance.list();
100:                List items = instance.list();
101:                for (int i = 0; i < items.size(); i++) {
102:                  ItemData item = (ItemData) items.get(i);
103:                  log.debug("Item #" + item.getItemId() + " has rationale= " +
104:                                     item.getHasRationale());
105:                }
106:              }
107:              if (args[0].equals("getQPItems")) {
108:                List items = instance.getQPItems(new Long(args[1])); // poolId
109:                for (int i = 0; i < items.size(); i++) {
110:                  ItemData item = (ItemData) items.get(i);
111:                  log.debug("Item #" + item.getItemId() + " has rationale= " +
112:                                     item.getHasRationale());
113:                }
114:              }
115:              System.exit(0);
116:            }
117:             */
118:
119:            public Long add() {
120:                ItemData item = new ItemData();
121:                item.setInstruction("Matching game");
122:                item.setTypeId(TypeFacade.MATCHING);
123:                item.setScore(new Float(10));
124:                item.setHasRationale(Boolean.FALSE);
125:                item.setStatus(new Integer(1));
126:                item.setCreatedBy("1");
127:                item.setCreatedDate(new Date());
128:                item.setLastModifiedBy("1");
129:                item.setLastModifiedDate(new Date());
130:
131:                // prepare itemText
132:                item.setItemTextSet(prepareText(item));
133:
134:                // prepare MetaData
135:                item.setItemMetaDataSet(prepareMetaData(item));
136:                item.addItemMetaData("ITEM_OBJECTIVE",
137:                        "the objective is to ...");
138:
139:                // prepare feedback
140:                item.setCorrectItemFeedback("well done!");
141:                item.setInCorrectItemFeedback("better luck next time!");
142:
143:                int retryCount = PersistenceService.getInstance()
144:                        .getRetryCount().intValue();
145:                while (retryCount > 0) {
146:                    try {
147:                        getHibernateTemplate().save(item);
148:                        retryCount = 0;
149:                    } catch (Exception e) {
150:                        log.warn("problem saving item: " + e.getMessage());
151:                        retryCount = PersistenceService.getInstance()
152:                                .retryDeadlock(e, retryCount);
153:                    }
154:                }
155:                return item.getItemId();
156:            }
157:
158:            public List getQPItems(final Long questionPoolId) {
159:                final HibernateCallback hcb = new HibernateCallback() {
160:                    public Object doInHibernate(Session session)
161:                            throws HibernateException, SQLException {
162:                        Query q = session
163:                                .createQuery("select ab from ItemData ab, QuestionPoolItem qpi where qpi.itemId=ab.itemIdString and qpi.questionPoolId = ?");
164:                        q.setLong(0, questionPoolId.longValue());
165:                        return q.list();
166:                    };
167:                };
168:                return getHibernateTemplate().executeFind(hcb);
169:
170:                //    return getHibernateTemplate().find("select ab from ItemData ab, QuestionPoolItem qpi where qpi.itemId=ab.itemIdString and qpi.questionPoolId = ?",
171:                //    		new Object[] { questionPoolId }, new org.hibernate.type.Type[] { Hibernate.LONG });
172:            }
173:
174:            public List list() {
175:                return getHibernateTemplate().find("from ItemData");
176:            }
177:
178:            public void show(Long itemId) {
179:                getHibernateTemplate().load(ItemData.class, itemId);
180:            }
181:
182:            public ItemFacade getItem(Long itemId, String agent) {
183:                ItemFacade item = new ItemFacade(
184:                        (ItemData) getHibernateTemplate().load(ItemData.class,
185:                                itemId));
186:                return item;
187:            }
188:
189:            public void showType(Long typeId) {
190:                getHibernateTemplate().load(TypeD.class, typeId);
191:            }
192:
193:            public void listType() {
194:                TypeFacadeQueriesAPI typeFacadeQueries = PersistenceService
195:                        .getInstance().getTypeFacadeQueries();
196:                TypeFacade f = typeFacadeQueries.getTypeFacadeById(new Long(1));
197:                log.debug("***facade: " + f.getAuthority());
198:            }
199:
200:            public void remove(Long itemId) {
201:                ItemData item = (ItemData) getHibernateTemplate().load(
202:                        ItemData.class, itemId);
203:
204:                // get list of attachment in section
205:                AssessmentService service = new AssessmentService();
206:                List itemAttachmentList = service.getItemResourceIdList(item);
207:                service.deleteResources(itemAttachmentList);
208:
209:                int retryCount = PersistenceService.getInstance()
210:                        .getRetryCount().intValue();
211:                while (retryCount > 0) {
212:                    try {
213:                        getHibernateTemplate().delete(item);
214:                        retryCount = 0;
215:                    } catch (Exception e) {
216:                        log.warn("problem deleting item : " + e.getMessage());
217:                        retryCount = PersistenceService.getInstance()
218:                                .retryDeadlock(e, retryCount);
219:                    }
220:                }
221:                if (item != null) {
222:                    printItem(item);
223:                }
224:            }
225:
226:            public void deleteItem(Long itemId, String agent) {
227:                ItemData item = (ItemData) getHibernateTemplate().load(
228:                        ItemData.class, itemId);
229:                // get list of attachment in item
230:                AssessmentService service = new AssessmentService();
231:                List itemAttachmentList = service.getItemResourceIdList(item);
232:                service.deleteResources(itemAttachmentList);
233:
234:                int retryCount = PersistenceService.getInstance()
235:                        .getRetryCount().intValue();
236:                while (retryCount > 0) {
237:                    try {
238:                        SectionDataIfc section = item.getSection();
239:                        // section might be null if you are deleting an item created inside a pool, that's not linked to any assessment. 
240:                        if (section != null) {
241:                            Set set = section.getItemSet();
242:                            set.remove(item);
243:                        }
244:                        getHibernateTemplate().delete(item);
245:                        retryCount = 0;
246:                    } catch (Exception e) {
247:                        log.warn("problem deleting item: " + e.getMessage());
248:                        retryCount = PersistenceService.getInstance()
249:                                .retryDeadlock(e, retryCount);
250:                    }
251:                }
252:            }
253:
254:            // is this used by ItemAddListener to save item? -daisyf
255:            public void deleteItemContent(Long itemId, String agent) {
256:                ItemData item = (ItemData) getHibernateTemplate().load(
257:                        ItemData.class, itemId);
258:
259:                int retryCount = PersistenceService.getInstance()
260:                        .getRetryCount().intValue();
261:                while (retryCount > 0) {
262:                    try {
263:                        if (item != null) { // need to dissociate with item before deleting in Hibernate 3
264:                            Set set = item.getItemTextSet();
265:                            item.setItemTextSet(new HashSet());
266:                            getHibernateTemplate().deleteAll(set);
267:                            retryCount = 0;
268:                        } else
269:                            retryCount = 0;
270:                    } catch (Exception e) {
271:                        log
272:                                .warn("problem deleteItemTextSet: "
273:                                        + e.getMessage());
274:                        retryCount = PersistenceService.getInstance()
275:                                .retryDeadlock(e, retryCount);
276:                    }
277:                }
278:
279:                retryCount = PersistenceService.getInstance().getRetryCount()
280:                        .intValue();
281:                while (retryCount > 0) {
282:                    try {
283:                        if (item != null) { // need to dissociate with item before deleting in Hibernate 3
284:                            Set set = item.getItemMetaDataSet();
285:                            item.setItemMetaDataSet(new HashSet());
286:                            getHibernateTemplate().deleteAll(set);
287:                            retryCount = 0;
288:                        } else
289:                            retryCount = 0;
290:                    } catch (Exception e) {
291:                        log.warn("problem deleteItemMetaDataSet: "
292:                                + e.getMessage());
293:                        retryCount = PersistenceService.getInstance()
294:                                .retryDeadlock(e, retryCount);
295:                    }
296:                }
297:
298:                retryCount = PersistenceService.getInstance().getRetryCount()
299:                        .intValue();
300:                while (retryCount > 0) {
301:                    try {
302:                        if (item != null) { // need to dissociate with item before deleting in Hibernate 3
303:                            Set set = item.getItemFeedbackSet();
304:                            item.setItemFeedbackSet(new HashSet());
305:                            getHibernateTemplate().deleteAll(set);
306:                            retryCount = 0;
307:                        } else
308:                            retryCount = 0;
309:                    } catch (Exception e) {
310:                        log.warn("problem deleting ItemFeedbackSet: "
311:                                + e.getMessage());
312:                        retryCount = PersistenceService.getInstance()
313:                                .retryDeadlock(e, retryCount);
314:                    }
315:                }
316:            }
317:
318:            public void deleteItemMetaData(final Long itemId, final String label) {
319:                // delete metadata by label
320:                ItemData item = (ItemData) getHibernateTemplate().load(
321:                        ItemData.class, itemId);
322:                final String query = "from ItemMetaData imd where imd.item.itemId=? and imd.label= ?";
323:
324:                final HibernateCallback hcb = new HibernateCallback() {
325:                    public Object doInHibernate(Session session)
326:                            throws HibernateException, SQLException {
327:                        Query q = session.createQuery(query);
328:                        q.setLong(0, itemId.longValue());
329:                        q.setString(1, label);
330:                        return q.list();
331:                    };
332:                };
333:                List itemmetadatalist = getHibernateTemplate().executeFind(hcb);
334:
335:                int retryCount = PersistenceService.getInstance()
336:                        .getRetryCount().intValue();
337:                while (retryCount > 0) {
338:                    try {
339:                        if (item != null) { // need to dissociate with item before deleting in Hibernate 3
340:                            Iterator iter = itemmetadatalist.iterator();
341:                            while (iter.hasNext()) {
342:                                ItemMetaDataIfc meta = (ItemMetaDataIfc) iter
343:                                        .next();
344:                                meta.setItem(null);
345:                            }
346:
347:                            Set set = item.getItemMetaDataSet();
348:                            set.removeAll(itemmetadatalist);
349:                            item.setItemMetaDataSet(set);
350:                            getHibernateTemplate().deleteAll(itemmetadatalist);
351:                            retryCount = 0;
352:                        } else
353:                            retryCount = 0;
354:                    } catch (Exception e) {
355:                        log.warn("problem delete itemmetadatalist: "
356:                                + e.getMessage());
357:                        retryCount = PersistenceService.getInstance()
358:                                .retryDeadlock(e, retryCount);
359:                    }
360:                }
361:            }
362:
363:            public void addItemMetaData(Long itemId, String label, String value) {
364:                ItemData item = (ItemData) getHibernateTemplate().load(
365:                        ItemData.class, itemId);
366:                if (item != null) {
367:                    printItem(item);
368:
369:                    ItemMetaData itemmetadata = new ItemMetaData(item, label,
370:                            value);
371:                    int retryCount = PersistenceService.getInstance()
372:                            .getRetryCount().intValue();
373:                    while (retryCount > 0) {
374:                        try {
375:                            getHibernateTemplate().save(itemmetadata);
376:                            retryCount = 0;
377:                        } catch (Exception e) {
378:                            log.warn("problem saving itemmetadata: "
379:                                    + e.getMessage());
380:                            retryCount = PersistenceService.getInstance()
381:                                    .retryDeadlock(e, retryCount);
382:                        }
383:                    }
384:                    //item.addItemMetaData(label, value);
385:                    //getHibernateTemplate().saveOrUpdate(item);
386:                }
387:            }
388:
389:            private HashSet prepareText(ItemData item) {
390:                HashSet textSet = new HashSet();
391:                ItemText text1 = new ItemText();
392:                text1.setItem(item);
393:                text1.setSequence(new Long(1));
394:                text1.setText("cat has");
395:                HashSet answerSet1 = new HashSet();
396:                HashSet answerFeedbackSet1 = new HashSet();
397:                Answer answer1 = new Answer(text1, "2 legs", new Long(1), "i",
398:                        Boolean.FALSE, null, new Float(0));
399:                answerFeedbackSet1.add(new AnswerFeedback(answer1, "incorrect",
400:                        "sorry"));
401:                answer1.setAnswerFeedbackSet(answerFeedbackSet1);
402:                answerSet1.add(answer1);
403:                answerSet1.add(new Answer(text1, "3 legs", new Long(2), "ii",
404:                        Boolean.FALSE, null, new Float(0)));
405:                answerSet1.add(new Answer(text1, "4 legs", new Long(3), "iii",
406:                        Boolean.TRUE, null, new Float(5)));
407:                text1.setAnswerSet(answerSet1);
408:
409:                textSet.add(text1);
410:
411:                ItemText text2 = new ItemText();
412:                text2.setItem(item);
413:                text2.setSequence(new Long(2));
414:                text2.setText("chicken has");
415:                HashSet answerSet2 = new HashSet();
416:                answerSet2.add(new Answer(text2, "2 legs", new Long(1), "i",
417:                        Boolean.TRUE, null, new Float(5)));
418:                answerSet2.add(new Answer(text2, "3 legs", new Long(2), "ii",
419:                        Boolean.FALSE, null, new Float(0)));
420:                answerSet2.add(new Answer(text2, "4 legs", new Long(3), "iii",
421:                        Boolean.FALSE, null, new Float(0)));
422:                text2.setAnswerSet(answerSet2);
423:                textSet.add(text2);
424:
425:                ItemText text3 = new ItemText();
426:                text3.setItem(item);
427:                text3.setSequence(new Long(3));
428:                text3.setText("baby has");
429:                HashSet answerSet3 = new HashSet();
430:                answerSet3.add(new Answer(text3, "2 legs", new Long(1), "i",
431:                        Boolean.FALSE, null, new Float(0)));
432:                answerSet3.add(new Answer(text3, "3 legs", new Long(2), "ii",
433:                        Boolean.FALSE, null, new Float(0)));
434:                answerSet3.add(new Answer(text3, "4 legs", new Long(3), "iii",
435:                        Boolean.TRUE, null, new Float(5)));
436:                text3.setAnswerSet(answerSet3);
437:                textSet.add(text3);
438:                return textSet;
439:            }
440:
441:            private HashSet prepareMetaData(ItemData item) {
442:                HashSet set = new HashSet();
443:                set.add(new ItemMetaData(item, "qmd_itemtype", "Matching"));
444:                set.add(new ItemMetaData(item, "TEXT_FORMAT", "HTML"));
445:                set.add(new ItemMetaData(item, "MUTUALLY_EXCLUSIVE", "True"));
446:                return set;
447:            }
448:
449:            private void printItem(ItemData item) {
450:                log.debug("**Id = " + item.getItemId());
451:                log.debug("**score = " + item.getScore());
452:                log.debug("**grade = " + item.getGrade());
453:                log.debug("**CorrectFeedback is lazy = "
454:                        + item.getCorrectItemFeedback());
455:                log.debug("**Objective not lazy = "
456:                        + item.getItemMetaDataByLabel("ITEM_OBJECTIVE"));
457:            }
458:
459:            public Long facadeAdd() throws DataFacadeException {
460:                ItemFacade item = new ItemFacade();
461:                item.setInstruction("Matching game");
462:                item.setTypeId(new Long(9));
463:                item.setScore(new Float(10));
464:                item.setHasRationale(Boolean.FALSE);
465:                item.setStatus(new Integer(1));
466:                item.setCreatedBy("1");
467:                item.setCreatedDate(new Date());
468:                item.setLastModifiedBy("1");
469:                item.setLastModifiedDate(new Date());
470:
471:                // prepare itemText
472:                item.setItemTextSet(prepareText((ItemData) item.getData()));
473:                item.addItemText("I have", new HashSet());
474:
475:                // prepare MetaData
476:                item.setItemMetaDataSet(prepareMetaData((ItemData) item
477:                        .getData()));
478:                item.addItemMetaData("ITEM_OBJECTIVE",
479:                        "the objective is to ...");
480:
481:                // prepare feedback
482:                item.setCorrectItemFeedback("well done!");
483:                item.setInCorrectItemFeedback("better luck next time!");
484:
485:                getHibernateTemplate().save(item.getData());
486:                return item.getData().getItemId();
487:            }
488:
489:            public void ifcShow(Long itemId) {
490:                ItemDataIfc itemData = (ItemDataIfc) getHibernateTemplate()
491:                        .load(ItemData.class, itemId);
492:                if (itemData != null) {
493:                    printIfcItem(itemData);
494:                    printFacadeItem(itemData);
495:                    //exportXml(itemData);
496:                }
497:            }
498:
499:            public ItemFacade saveItem(ItemFacade item)
500:                    throws DataFacadeException {
501:                try {
502:                    ItemData itemdata = (ItemData) item.getData();
503:                    itemdata.setLastModifiedDate(new Date());
504:                    itemdata.setLastModifiedBy(AgentFacade.getAgentString());
505:                    int retryCount = PersistenceService.getInstance()
506:                            .getRetryCount().intValue();
507:                    while (retryCount > 0) {
508:                        try {
509:                            getHibernateTemplate().saveOrUpdate(itemdata);
510:                            item.setItemId(itemdata.getItemId());
511:                            retryCount = 0;
512:                        } catch (Exception e) {
513:                            log.warn("problem save or update itemdata: "
514:                                    + e.getMessage());
515:                            retryCount = PersistenceService.getInstance()
516:                                    .retryDeadlock(e, retryCount);
517:                        }
518:                    }
519:                    if ((item.getData() != null)
520:                            && (item.getData().getSection() != null)) {
521:                        AssessmentIfc assessment = item.getData().getSection()
522:                                .getAssessment();
523:                        assessment.setLastModifiedBy(AgentFacade
524:                                .getAgentString());
525:                        assessment.setLastModifiedDate(new Date());
526:                        retryCount = PersistenceService.getInstance()
527:                                .getRetryCount().intValue();
528:                        while (retryCount > 0) {
529:                            try {
530:                                getHibernateTemplate().update(assessment);
531:                                retryCount = 0;
532:                            } catch (Exception e) {
533:                                log.warn("problem updating asssessment: "
534:                                        + e.getMessage());
535:                                retryCount = PersistenceService.getInstance()
536:                                        .retryDeadlock(e, retryCount);
537:                            }
538:                        }
539:                    }
540:                    return item;
541:                } catch (Exception e) {
542:                    e.printStackTrace();
543:                    return null;
544:                }
545:            }
546:
547:            private void printIfcItem(ItemDataIfc item) {
548:                log.debug("**Id = " + item.getItemId());
549:                log.debug("**score = " + item.getScore());
550:                log.debug("**grade = " + item.getGrade());
551:                log.debug("**CorrectFeedback is lazy = "
552:                        + item.getCorrectItemFeedback());
553:                log.debug("**Objective not lazy = "
554:                        + item.getItemMetaDataByLabel("ITEM_OBJECTIVE"));
555:                log.debug("**createdDate = " + item.getCreatedDate());
556:            }
557:
558:            private void printFacadeItem(ItemDataIfc item) {
559:                ItemFacade f = new ItemFacade(item);
560:                log.debug("****Id = " + f.getItemId());
561:                log.debug("****score = " + f.getScore());
562:                log.debug("****grade = " + f.getGrade());
563:                log.debug("****CorrectFeedback is lazy = "
564:                        + f.getCorrectItemFeedback());
565:                log.debug("****Objective not lazy = "
566:                        + f.getItemMetaDataByLabel("ITEM_OBJECTIVE"));
567:                log.debug("****createdDate = " + f.getCreatedDate());
568:                log.debug("****ItemType = " + f.getItemType().getKeyword());
569:            }
570:
571:            /**
572:            private void exportXml(ItemDataIfc item) {
573:            XStream xstream = new XStream();
574:            xstream = new XStream(new DomDriver());
575:            xstream.alias("item", ItemData.class);
576:            xstream.alias("itemText", ItemText.class);
577:            xstream.alias("itemFeedback", ItemFeedback.class);
578:            xstream.alias("itemMetaData", ItemMetaData.class);
579:            xstream.alias("answer", Answer.class);
580:            xstream.alias("answerFeedback", AnswerFeedback.class);
581:            String xml = xstream.toXML(item);
582:            byte[] b = xml.getBytes();
583:            try {
584:              FileOutputStream out = new FileOutputStream("out");
585:              out.write(b);
586:            }
587:            catch (FileNotFoundException ex) {
588:            }
589:            catch (IOException ex1) {
590:            }
591:            }
592:             */
593:
594:            public ItemFacade getItem(Long itemId) {
595:                ItemData item = (ItemData) getHibernateTemplate().load(
596:                        ItemData.class, itemId);
597:                return new ItemFacade(item);
598:            }
599:
600:            public HashMap getItemsByKeyword(final String keyword) {
601:                final HibernateCallback hcb = new HibernateCallback() {
602:                    public Object doInHibernate(Session session)
603:                            throws HibernateException, SQLException {
604:                        Query q = session
605:                                .createQuery("select ab from ItemData ab, ItemText itext where itext.item=ab and itext.text like ? ");
606:                        q.setString(0, keyword);
607:                        return q.list();
608:                    };
609:                };
610:                List list1 = getHibernateTemplate().executeFind(hcb);
611:
612:                //     List list1 = getHibernateTemplate().find("select ab from ItemData ab, ItemText itext where itext.item=ab and itext.text like ? ",new Object[] { keyword}, new org.hibernate.type.Type[] { Hibernate.STRING });
613:
614:                final HibernateCallback hcb2 = new HibernateCallback() {
615:                    public Object doInHibernate(Session session)
616:                            throws HibernateException, SQLException {
617:                        Query q = session
618:                                .createQuery("select distinct ab from ItemData ab, Answer answer where answer.item=ab and answer.text like ? ");
619:                        q.setString(0, keyword);
620:                        return q.list();
621:                    };
622:                };
623:                List list2 = getHibernateTemplate().executeFind(hcb2);
624:
625:                //     List list2 = getHibernateTemplate().find("select distinct ab from ItemData ab, Answer answer where answer.item=ab and answer.text like ? ",new Object[] { keyword}, new org.hibernate.type.Type[] { Hibernate.STRING });
626:
627:                final HibernateCallback hcb3 = new HibernateCallback() {
628:                    public Object doInHibernate(Session session)
629:                            throws HibernateException, SQLException {
630:                        Query q = session
631:                                .createQuery("select ab from ItemData ab, ItemMetaData md where md.item=ab and md.entry like ?  and md.label= 'KEYWORD' ");
632:                        q.setString(0, keyword);
633:                        return q.list();
634:                    };
635:                };
636:                List list3 = getHibernateTemplate().executeFind(hcb3);
637:
638:                //     List list3 = getHibernateTemplate().find("select ab from ItemData ab, ItemMetaData metadata where metadata.item=ab and metadata.entry like ?  and metadata.label= 'KEYWORD' ", new Object[] { keyword}, new org.hibernate.type.Type[] { Hibernate.STRING });
639:
640:                final HibernateCallback hcb4 = new HibernateCallback() {
641:                    public Object doInHibernate(Session session)
642:                            throws HibernateException, SQLException {
643:                        Query q = session
644:                                .createQuery("select ab from ItemData ab where ab.instruction like ?  ");
645:                        q.setString(0, keyword);
646:                        return q.list();
647:                    };
648:                };
649:                List list4 = getHibernateTemplate().executeFind(hcb4);
650:
651:                //     List list4 = getHibernateTemplate().find("select ab from ItemData ab where ab.instruction like ?  ", new Object[] { keyword}, new org.hibernate.type.Type[] { Hibernate.STRING });
652:                HashMap itemfacadeMap = new HashMap();
653:
654:                for (int i = 0; i < list1.size(); i++) {
655:                    ItemData a = (ItemData) list1.get(i);
656:                    ItemFacade f = new ItemFacade(a);
657:                    itemfacadeMap.put(f.getItemIdString(), f);
658:                }
659:                for (int i = 0; i < list2.size(); i++) {
660:                    ItemData a = (ItemData) list2.get(i);
661:                    ItemFacade f = new ItemFacade(a);
662:                    itemfacadeMap.put(f.getItemIdString(), f);
663:                }
664:                for (int i = 0; i < list3.size(); i++) {
665:                    ItemData a = (ItemData) list3.get(i);
666:                    ItemFacade f = new ItemFacade(a);
667:                    itemfacadeMap.put(f.getItemIdString(), f);
668:                }
669:                for (int i = 0; i < list4.size(); i++) {
670:                    ItemData a = (ItemData) list4.get(i);
671:                    ItemFacade f = new ItemFacade(a);
672:                    itemfacadeMap.put(f.getItemIdString(), f);
673:                }
674:
675:                log.debug("Search for keyword, found: " + itemfacadeMap.size());
676:                return itemfacadeMap;
677:
678:            }
679:
680:            /*
681:             * This API is for linear access to create a dummy record to indicate the student
682:             * has taken action on the item (question). Therefore, we just need one itemTextId
683:             * for recording - use the first one (index 0).
684:             */
685:            public Long getItemTextId(final Long publishedItemId) {
686:                final HibernateCallback hcb = new HibernateCallback() {
687:                    public Object doInHibernate(Session session)
688:                            throws HibernateException, SQLException {
689:                        Query q = session
690:                                .createQuery("select i.id from PublishedItemText i where i.item.itemId = ?");
691:                        q.setLong(0, publishedItemId.longValue());
692:                        return q.list();
693:                    };
694:                };
695:                List list = getHibernateTemplate().executeFind(hcb);
696:                log.debug("list.size() = " + list.size());
697:                Long itemTextId = (Long) list.get(0);
698:                log.debug("itemTextId" + itemTextId);
699:                return itemTextId;
700:            }
701:
702:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.