Java tutorial
/** * PureInfo Command * @(#)InstituteQueryAction.java 1.0 2007-7-18 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.reports.table.data.institute; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.apache.struts.action.ActionForward; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.XMLWriter; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.ark.interaction.ActionBase; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.org.domain.IOrganizationMgr; import com.pureinfo.srm.org.model.Organization; /** * <P> * Created on 2007-7-18 14:26:09 <BR> * Last modified on 2007-7-18 * </P> * TODO describe InstituteQueryAction here ... * * @author elmar.chen * @version 1.0, 2007-7-18 * @since Command 1.0 */ public class InstituteQueryAction extends ActionBase { /** * @see com.pureinfo.ark.interaction.ActionBase#isLoginNeeded() */ protected boolean isLoginNeeded() { return false; } /** * @see com.pureinfo.ark.interaction.ActionBase#executeAction() */ public ActionForward executeAction() throws PureException { int nCollegeId = request.getRequiredInt("college", "ID"); IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class); List listIns = mgr.getChildren(nCollegeId, true); Document sInsJson = makeXML(listIns); writeResponse(sInsJson); return null; } private void writeResponse(Document sInsJson) throws PureException { try { response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1 response.setHeader("Pragma", "no-cache"); //HTTP 1.0 response.setDateHeader("Expires", -1); response.setDateHeader("max-age", 0); response.setContentType("text/xml"); response.setCharacterEncoding("utf-8"); XMLWriter writer = new XMLWriter(response.getWriter()); writer.write(sInsJson); writer.close(); } catch (IOException e) { throw new PureException(PureException.FILE_WRITE_FAILED, "failed to write response", e); } } private Document makeXML(List _sListIns) { Document doc = DocumentHelper.createDocument(); Element xRoot = doc.addElement("datas"); Element inses = xRoot.addElement("inses"); for (Iterator iter = _sListIns.iterator(); iter.hasNext();) { Organization org = (Organization) iter.next(); int id = org.getId(); String sName = org.getName(); Element xIns = inses.addElement("ins"); xIns.addAttribute("id", Integer.toString(id)); xIns.addAttribute("name", sName); xIns.addAttribute("code", org.getCode()); } return doc; } }