001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/qti/asi/Assessment.java $
003: * $Id: Assessment.java 9274 2006-05-10 22:50:48Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 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.qti.asi;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Map;
027:
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.parsers.ParserConfigurationException;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036:
037: import org.sakaiproject.tool.assessment.qti.constants.QTIConstantStrings;
038:
039: /**
040: * <p>Copyright: Copyright (c) 2003-5</p>
041: * <p>Organization: Sakai Project</p>
042: * @author casong
043: * @author Ed Smiley esmiley@stanford.edu
044: * @version $Id: Assessment.java 9274 2006-05-10 22:50:48Z daisyf@stanford.edu $
045: */
046: public class Assessment extends ASIBaseClass {
047: private static Log log = LogFactory.getLog(Assessment.class);
048: private String basePath;
049: private Map sections;
050: private Map items;
051:
052: /**
053: * Explicitly setting serialVersionUID insures future versions can be
054: * successfully restored. It is essential this variable name not be changed
055: * to SERIALVERSIONUID, as the default serialization methods expects this
056: * exact name.
057: */
058: private static final long serialVersionUID = 1;
059:
060: /**
061: * Creates a new Assessment object.
062: */
063: protected Assessment() {
064: super ();
065: this .sections = new HashMap();
066: this .items = new HashMap();
067: this .basePath = QTIConstantStrings.QUESTESTINTEROP + "/"
068: + QTIConstantStrings.ASSESSMENT;
069: }
070:
071: /**
072: * Creates a new Assessment object.
073: *
074: * @param document the Document containing the assessment.
075: */
076: public Assessment(Document document) {
077: super (document);
078: this .sections = new HashMap();
079: this .items = new HashMap();
080: this .basePath = QTIConstantStrings.QUESTESTINTEROP + "/"
081: + QTIConstantStrings.ASSESSMENT;
082: }
083:
084: /**
085: * Method for meta data.
086: *
087: * @todo use QTIConstantStrings:
088: * //QTIMetaData
089: public static String QTIMETADATAFIELD = "qtimetadatafield";
090: public static String FIELDLABEL = "fieldlabel";
091: public static String FIELDENTRY = "fieldentry";
092:
093: *
094: * @param fieldlabel the fieldlabel
095: *
096: * @return the entry
097: */
098: public String getFieldentry(String fieldlabel) {
099: if (log.isDebugEnabled()) {
100: log.debug("getFieldentry(String " + fieldlabel + ")");
101: }
102:
103: String xpath = basePath
104: + "/qtimetadata/qtimetadatafield/fieldlabel[text()='"
105: + fieldlabel + "']/following-sibling::fieldentry";
106:
107: return super .getFieldentry(xpath);
108: }
109:
110: /**
111: *
112: *
113: * @param fieldlabel
114: * @param setValue
115: * @param noEscapeXML
116: */
117: public void setFieldentry(String fieldlabel, String setValue) {
118: setFieldentry(fieldlabel, setValue, false);
119: }
120:
121: /**
122: *
123: *
124: * @param fieldlabel
125: * @param setValue
126: */
127: public void setFieldentry(String fieldlabel, String setValue,
128: boolean noEscapeXML) {
129: if (log.isDebugEnabled()) {
130: log.debug("setFieldentry(String " + fieldlabel
131: + ", String " + setValue + ")");
132: }
133:
134: String xpath = "questestinterop/assessment/qtimetadata/qtimetadatafield/fieldlabel[text()='"
135: + fieldlabel + "']/following-sibling::fieldentry";
136: super .setFieldentry(xpath, setValue, noEscapeXML);
137: }
138:
139: /**
140: * Add a section ref with section Id sectionId.
141: *
142: * @param sectionId
143: */
144: public void addSectionRef(String sectionId) {
145: if (log.isDebugEnabled()) {
146: log.debug("addSection(String " + sectionId + ")");
147: }
148:
149: try {
150: String xpath = basePath;
151: DocumentBuilderFactory dbf = DocumentBuilderFactory
152: .newInstance();
153: DocumentBuilder db = dbf.newDocumentBuilder();
154: Document document = db.newDocument();
155: Element element = document
156: .createElement(QTIConstantStrings.SECTIONREF);
157: element.setAttribute(QTIConstantStrings.LINKREFID,
158: sectionId);
159: this .addElement(xpath, element);
160: } catch (ParserConfigurationException pce) {
161: log.error("Exception thrown from addSectionRef() : "
162: + pce.getMessage());
163: pce.printStackTrace();
164: }
165: }
166:
167: /**
168: * Remove a section ref with section Id sectionId.
169: *
170: * @param sectionId
171: */
172: public void removeSectionRef(String sectionId) {
173: if (log.isDebugEnabled()) {
174: log.debug("removeSectionRef(String " + sectionId + ")");
175: }
176:
177: String xpath = basePath + "/" + QTIConstantStrings.SECTIONREF
178: + "[@" + QTIConstantStrings.LINKREFID + "='"
179: + sectionId + "']";
180: this .removeElement(xpath);
181: }
182:
183: /**
184: * Remove all section refs.
185: */
186: public void removeSectionRefs() {
187: log.debug("removeSectionRefs()");
188: String xpath = basePath + "/" + QTIConstantStrings.SECTIONREF;
189: this .removeElement(xpath);
190: }
191:
192: /**
193: * Get a collection of section refs.
194: *
195: * @return
196: */
197: public List getSectionRefs() {
198: log.debug("getSectionRefs()");
199: String xpath = basePath + "/" + QTIConstantStrings.SECTIONREF;
200:
201: return this .selectNodes(xpath);
202: }
203:
204: /**
205: * Get a collection of sections.
206: * @return the sections
207: */
208: public Collection getSections() {
209: return this .sections.values();
210: }
211:
212: /**
213: * Get a collection of items.
214: * @return the items
215: */
216: public Collection getItems() {
217: return this .items.values();
218: }
219:
220: /**
221: *
222: *
223: * @return
224: */
225: public List getSectionRefIds() {
226: log.debug("getSectionRefIds()");
227: List refs = this .getSectionRefs();
228: List ids = new ArrayList();
229: int size = refs.size();
230: for (int i = 0; i < size; i++) {
231: Element ref = (Element) refs.get(0);
232: String idString = ref
233: .getAttribute(QTIConstantStrings.LINKREFID);
234: ids.add(idString);
235: }
236:
237: return ids;
238: }
239:
240: /**
241: * Assessment title.
242: * @return title
243: */
244: public String getTitle() {
245: String title = "";
246: String xpath = basePath;
247: List list = this .selectNodes(xpath);
248: if (list.size() > 0) {
249: Element element = (Element) list.get(0);
250: title = element.getAttribute("title");
251: }
252: return title;
253: }
254:
255: /**
256: * Assessment id (ident attribute)
257: * @return ident
258: */
259: public String getIdent() {
260: String ident = "";
261: String xpath = basePath;
262: List list = this .selectNodes(xpath);
263: if (list.size() > 0) {
264: Element element = (Element) list.get(0);
265: ident = element.getAttribute("ident");
266: }
267: return ident;
268: }
269:
270: /**
271: * Assessment id (ident attribute)
272: * @param ident the ident
273: */
274: public void setIdent(String ident) {
275: String xpath = basePath;
276: List list = this .selectNodes(xpath);
277: if (list.size() > 0) {
278: Element element = (Element) list.get(0);
279: element.setAttribute("ident", ident);
280: }
281: }
282:
283: /**
284: * Assessment title.
285: * @param title
286: */
287: public void setTitle(String title) {
288: String xpath = basePath;
289: List list = this .selectNodes(xpath);
290: if (list.size() > 0) {
291: Element element = (Element) list.get(0);
292: element.setAttribute("title", escapeXml(title));
293: }
294: }
295:
296: /**
297: * Base XPath for the assessment.
298: * @return
299: */
300: public String getBasePath() {
301: return basePath;
302: }
303:
304: /**
305: * Base XPath for the assessment.
306: * @param basePath
307: */
308: public void setBasePath(String basePath) {
309: this.basePath = basePath;
310: }
311: }
|