/* * ################################################################ * * FenixEdu: The Java(TM) Object-Oriented Framework for University * Academic Applications * * Copyright (C) 2002-2003 IST/Technical University of Lisbon * Contact: suporte@dot.ist.utl.pt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * * Initial developer(s): The Fenix Team * http://fenix-ashes.ist.utl.pt/ * * Contributor(s): José Pedro Pereira - Educare S.A. - http://www.linkare.com * * ################################################################ */ package net.sourceforge.fenixedu.presentationTier.servlets; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sourceforge.fenixedu.domain.RootDomainObject; import pt.ist.fenixframework.DomainObject; import pt.ist.fenixframework.FenixFramework; import pt.ist.fenixframework.pstm.MissingObjectException; import pt.ist.fenixframework.pstm.Transaction; import com.linkare.commons.metainfo.Linkare; import dml.DomainClass; import dml.DomainEntity; import dml.DomainModel; import dml.Role; import dml.Slot; public class DomainBrowserServlet extends HttpServlet { private static final String DESCRIPTION = "Description"; private static DomainModel domainModel; private static HashMap domainClassesDescAttr = new HashMap(); public void init(ServletConfig config) throws ServletException { super.init(config); try { domainModel = FenixFramework.getDomainModel(); } catch (Exception e) { domainModel = null; } } @Linkare(author = "Paulo Zenida", comments = "Included the CSS and Javascript files in the HTML to deal with the sorting in the lists") public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("\n"); out.println("\n"); out.println(""); out.println("Fenix Domain Browser"); out.println(""); out.println("\n"); String path = req.getPathInfo(); try { if ("/listAll".equals(path)) { renderListAll(out, RequestParams.parse(req, "domainClass")); } else if ("/showObj".equals(path)) { renderDomainObject(out, RequestParams.parse(req, "OID")); } else if ("/listRole".equals(path)) { renderDomainObjectRole(out, RequestParams.parse(req, "OID", "role")); } else if ("/checkDomain".equals(path)) { checkDomain(out); } else { renderMainIndex(out); } } catch (MissingObjectException moe) { out.println("

No such object

"); } catch (Exception e) { e.printStackTrace(); throw new Error("Error: " + e.getMessage()); } out.println("\n"); out.println("\n"); out.close(); } protected void renderDomainObject(PrintWriter out, RequestParams params) throws IOException, Exception { if (params == null) { renderMainIndex(out); return; } DomainObject domObj = Transaction.getObjectForOID(params.oid); DomainClass domClass = getDomainClass(domObj); out.println("

"); out.println(domObj.getClass().getName()); out.println(" Instance: "); out.println(getObjectDescription(domObj)); out.println("

\n"); out.println("

Attributes

\n"); out.println("\n"); for (String attr : getAllAttrs(domClass)) { if (!isForeignKey(attr, domClass)) { out.println(""); } } out.println("
"); out.println(attr); out.println(""); out.println(getAttributeValue(domObj, attr)); out.println("
\n"); out.println("

Relations

"); out.println("\n"); for (Role role : getAllRoles(domClass)) { out.println(""); } out.println("
"); out.println(role.getName()); out.println(""); Object roleValue = getAttributeValue(domObj, role.getName()); if (role.getMultiplicityUpper() == 1) { if (roleValue == null) { out.println("null"); } else { DomainEntity roleType = role.getType(); if (roleType instanceof DomainClass) { renderObjectId(out, getDomainObject(roleValue)); } else { out.println(roleValue); } } } else { if (roleValue == null) { out.println("null collection"); } else { Collection col = (Collection) roleValue; int colSize = -1; if (params.showColSize) { try { colSize = col.size(); } catch (Exception e) { // ignore it } } out.println(""); out.println(role.getType().getFullName()); out.println("["); out.println(colSize == -1 ? "?" : colSize); out.println("]"); } } out.println("
\n"); } private boolean isForeignKey(String attrName, DomainClass domClass) { return (attrName.startsWith("key") && (domClass.findRoleSlot(firstCharToLower(attrName.substring(3))) != null)); } protected void renderDomainObjectRole(PrintWriter out, RequestParams params) throws IOException, Exception { if (params == null) { renderMainIndex(out); return; } DomainObject domObj = Transaction.getObjectForOID(params.oid); out.println("

"); renderObjectId(out, domObj); out.println("'s "); out.println(params.roleName); out.println(":

\n"); Collection insts = (Collection) getAttributeValue(domObj, params.roleName); renderCollection(out, insts, params); } protected void renderListAll(PrintWriter out, RequestParams params) throws IOException, Exception { if (params == null) { renderMainIndex(out); return; } out.println("

"); out.println(params.classFullName); out.println(" Entities

\n"); Collection insts = RootDomainObject.readAllDomainObjects(params.javaClass); renderCollection(out, insts, params); } @Linkare(author = "Paulo Zenida", comments = "Changed the renderization of the collection of objects from