| 1 | /** |
|---|
| 2 | * |
|---|
| 3 | */ |
|---|
| 4 | package org.rapla.plugin.xmlexport; |
|---|
| 5 | |
|---|
| 6 | import java.io.IOException; |
|---|
| 7 | import java.util.Date; |
|---|
| 8 | import java.util.Map; |
|---|
| 9 | import java.util.ArrayList; |
|---|
| 10 | import java.text.SimpleDateFormat; |
|---|
| 11 | import javax.servlet.ServletContext; |
|---|
| 12 | import javax.servlet.ServletException ; |
|---|
| 13 | import javax.servlet.http.HttpServletRequest; |
|---|
| 14 | import javax.servlet.http.HttpServletResponse; |
|---|
| 15 | |
|---|
| 16 | import org.rapla.entities.User; |
|---|
| 17 | import org.rapla.entities.Category; |
|---|
| 18 | import org.rapla.entities.domain.Reservation; |
|---|
| 19 | import org.rapla.entities.domain.Appointment; |
|---|
| 20 | import org.rapla.entities.domain.AppointmentBlockArray; |
|---|
| 21 | import org.rapla.entities.dynamictype.DynamicType ; |
|---|
| 22 | import org.rapla.entities.dynamictype.ClassificationFilter; |
|---|
| 23 | import org.rapla.entities.dynamictype.Classification; |
|---|
| 24 | import org.rapla.entities.dynamictype.Attribute; |
|---|
| 25 | import org.rapla.entities.dynamictype.AttributeType; |
|---|
| 26 | import org.rapla.entities.storage.RefEntity; |
|---|
| 27 | import org.rapla.components.util.IOUtil; |
|---|
| 28 | import org.rapla.components.util.SerializableDateTimeFormat; |
|---|
| 29 | import org.rapla.framework.RaplaContext; |
|---|
| 30 | import org.rapla.facade.*; |
|---|
| 31 | import org.rapla.framework.RaplaException; |
|---|
| 32 | import org.rapla.servletpages.RaplaPageGenerator ; |
|---|
| 33 | |
|---|
| 34 | import org.rapla.entities.domain.Allocatable ; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | public class XMLPageGenerator extends RaplaComponent implements RaplaPageGenerator |
|---|
| 38 | { |
|---|
| 39 | public XMLPageGenerator(RaplaContext context) throws RaplaException |
|---|
| 40 | { |
|---|
| 41 | super( context ); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public void generatePage( ServletContext context, HttpServletRequest request, HttpServletResponse response ) |
|---|
| 45 | throws IOException, ServletException |
|---|
| 46 | { |
|---|
| 47 | java.io.PrintWriter out = response.getWriter(); |
|---|
| 48 | try |
|---|
| 49 | { |
|---|
| 50 | /* Http Arguments : |
|---|
| 51 | * http://127.0.0.1:8051/rapla?page=xmlexport&user=admin&start=2007-06-01&end=2007-06-29 |
|---|
| 52 | * user : user who exported, admin |
|---|
| 53 | * start : start date in YYY-MM-DD format |
|---|
| 54 | * end : end date in YYY-MM-DD format |
|---|
| 55 | * keys : event or resource types separated by ',' (keys=defaultReservation,) |
|---|
| 56 | */ |
|---|
| 57 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|---|
| 58 | // Pass request for treatment |
|---|
| 59 | AppointmentBlockArray blocks = getBlocksForRequest(request, out); |
|---|
| 60 | |
|---|
| 61 | response.setContentType ( "text/xml; charset=ISO-8859-15" ); |
|---|
| 62 | out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-15\" ?>"); |
|---|
| 63 | out.println("<export>"); |
|---|
| 64 | |
|---|
| 65 | Map parameters = request.getParameterMap(); |
|---|
| 66 | String outputFormat = (parameters.containsKey("format") == true)? request.getParameter("format") : "default" ; |
|---|
| 67 | |
|---|
| 68 | for ( int i=0;i< blocks.size();i++) |
|---|
| 69 | { |
|---|
| 70 | out.println(" <block contextid=\"" + i + "\">"); |
|---|
| 71 | Appointment app = blocks.getAppointmentAt( i ); |
|---|
| 72 | Reservation reservation = app.getReservation(); |
|---|
| 73 | |
|---|
| 74 | out.println(" <start>" + sdf.format( new Date(blocks.getStartAt(i)) ) + "</start>"); |
|---|
| 75 | out.println(" <end>" + sdf.format( new Date(blocks.getEndAt(i)) ) + "</end>"); |
|---|
| 76 | out.println(" <name>" + reservation.getName(getLocale()).replaceAll("&", "-") + "</name>"); |
|---|
| 77 | |
|---|
| 78 | Allocatable[] persons = reservation.getPersons(); |
|---|
| 79 | Allocatable[] resources = reservation.getResources(); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | if ( outputFormat.equals( new String("limited") ) == true ) |
|---|
| 84 | { |
|---|
| 85 | this.allocatableToLimitedXML(persons, true, out, app); |
|---|
| 86 | this.allocatableToLimitedXML(resources, false, out, app); |
|---|
| 87 | } |
|---|
| 88 | else |
|---|
| 89 | { |
|---|
| 90 | this.allocatableToXML(persons, true, out, app); |
|---|
| 91 | this.allocatableToXML(resources, false, out, app); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | out.println(" </block>"); |
|---|
| 96 | } |
|---|
| 97 | out.println("</export>"); |
|---|
| 98 | |
|---|
| 99 | } |
|---|
| 100 | catch ( Exception ex ) |
|---|
| 101 | { |
|---|
| 102 | out.println( IOUtil.getStackTraceAsString ( ex ) ); |
|---|
| 103 | throw new ServletException( ex ); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | private AppointmentBlockArray getBlocksForRequest(HttpServletRequest request, java.io.PrintWriter out) throws ServletException { |
|---|
| 109 | Map parameters = request.getParameterMap(); |
|---|
| 110 | SerializableDateTimeFormat format = new SerializableDateTimeFormat(); |
|---|
| 111 | //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|---|
| 112 | |
|---|
| 113 | try { |
|---|
| 114 | String username = (parameters.containsKey("user"))? request.getParameter( "user" ) : "admin"; |
|---|
| 115 | Date start = new Date(); |
|---|
| 116 | Date end = new Date(start.getTime() + 86400*1000); |
|---|
| 117 | if (parameters.containsKey("start") == true) { |
|---|
| 118 | String startString = request.getParameter ( "start" ); |
|---|
| 119 | start = format.parseDate( startString, false ); |
|---|
| 120 | } |
|---|
| 121 | if (parameters.containsKey("end") == true) { |
|---|
| 122 | String endString = request.getParameter ( "end" ); |
|---|
| 123 | end = format.parseDate( endString, true); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | User user = getQuery().getUser( username ); |
|---|
| 127 | |
|---|
| 128 | // Filtering Resources |
|---|
| 129 | ArrayList filters = new ArrayList(); |
|---|
| 130 | if (parameters.containsKey("keys")) |
|---|
| 131 | { |
|---|
| 132 | String[] eventTypes = request.getParameter("keys").split(","); |
|---|
| 133 | for (int i=0;i<eventTypes.length ;i++) |
|---|
| 134 | { |
|---|
| 135 | DynamicType oneType = getQuery().getDynamicType(eventTypes[i]); |
|---|
| 136 | ClassificationFilter oneFilter = oneType.newClassificationFilter(); |
|---|
| 137 | filters.add( oneFilter ); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | ClassificationFilter[] cfilters = new ClassificationFilter[filters.size()]; |
|---|
| 141 | for (int i=0;i< filters.size ();i++) |
|---|
| 142 | cfilters[i] = (ClassificationFilter) filters.get(i); |
|---|
| 143 | Allocatable[] allocatables = getQuery().getAllocatables(cfilters); |
|---|
| 144 | |
|---|
| 145 | // get events respecting http arguments and resulting filtered resources |
|---|
| 146 | Reservation[] events = getQuery().getReservations(allocatables, start, end); |
|---|
| 147 | |
|---|
| 148 | // build the blocks |
|---|
| 149 | AppointmentBlockArray blocks = new AppointmentBlockArray(); |
|---|
| 150 | |
|---|
| 151 | for ( int i=0;i<events.length;i++) |
|---|
| 152 | { |
|---|
| 153 | Reservation event = events[i]; |
|---|
| 154 | Appointment[] appointments = event.getAppointments(); |
|---|
| 155 | for ( int j=0;j< appointments.length;j++) |
|---|
| 156 | { |
|---|
| 157 | Appointment appointment = appointments[j]; |
|---|
| 158 | appointment.createBlocks ( start, end, blocks ); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | // sort them by time |
|---|
| 162 | blocks.sort(); |
|---|
| 163 | |
|---|
| 164 | return blocks; |
|---|
| 165 | |
|---|
| 166 | } |
|---|
| 167 | catch ( Exception ex ) |
|---|
| 168 | { |
|---|
| 169 | out.println( IOUtil.getStackTraceAsString ( ex ) ); |
|---|
| 170 | throw new ServletException( ex ); |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * allocatableToXML loops through an array of allocatables, fetches every attributes, and outputs in xml |
|---|
| 176 | * |
|---|
| 177 | */ |
|---|
| 178 | public void allocatableToXML(Allocatable[] alls, boolean isPerson, java.io.PrintWriter out, Appointment appointment) { |
|---|
| 179 | String blockName = (isPerson)? "person" : "resource"; |
|---|
| 180 | if (alls.length > 0) { |
|---|
| 181 | out.println(" <" + blockName + "s>"); |
|---|
| 182 | for (int j=0;j<alls.length;j++) { |
|---|
| 183 | |
|---|
| 184 | boolean hasAlloc=appointment.getReservation().hasAllocated(alls[j],appointment); |
|---|
| 185 | if(hasAlloc) |
|---|
| 186 | { |
|---|
| 187 | Classification classification = alls[j].getClassification(); |
|---|
| 188 | String allKey = classification.getType().getElementKey(); |
|---|
| 189 | Attribute[] attributes = classification.getAttributes(); |
|---|
| 190 | String allocatableId = ((RefEntity) alls[j]).getId().toString(); |
|---|
| 191 | if ( allocatableId.contains("_") == true) { |
|---|
| 192 | allocatableId = allocatableId.split("_")[1]; |
|---|
| 193 | } |
|---|
| 194 | out.println(" <" + blockName + " typekey='" + allKey + "' relid='" + allocatableId + "' >"); |
|---|
| 195 | out.println(" <" + blockName + "Type>" + classification.getType().getName(getLocale()) + "</" + blockName + "Type>"); |
|---|
| 196 | out.println(" <displayName>" + alls[j].getName(getLocale()).replace("&", "") + "</displayName>"); |
|---|
| 197 | for (int k=0; k<attributes.length; k++) |
|---|
| 198 | { |
|---|
| 199 | String attributeKey = attributes[k].getKey(); |
|---|
| 200 | if (attributes[k].getType() == AttributeType.CATEGORY) { |
|---|
| 201 | Category cat = (Category) classification.getValue(attributeKey); |
|---|
| 202 | if ( cat != null ) { |
|---|
| 203 | out.println(" <" + attributeKey + ">" + cat.getName(getLocale()).replace("&", "") + "</" + attributeKey + ">"); |
|---|
| 204 | } |
|---|
| 205 | else { |
|---|
| 206 | out.println(" <" + attributeKey + ">null</" + attributeKey + ">"); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | else { |
|---|
| 210 | Object attributeValue = classification.getValue(attributeKey); |
|---|
| 211 | if ( attributeValue != null) { |
|---|
| 212 | out.println(" <" + attributeKey + ">" + attributeValue.toString().replace("&", "") + "</" + attributeKey + ">"); |
|---|
| 213 | } |
|---|
| 214 | else { |
|---|
| 215 | out.println(" <" + attributeKey + ">null</" + attributeKey + ">"); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | out.println(" </" + blockName + ">"); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | out.println(" </" + blockName + "s>"); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /** |
|---|
| 227 | * allocatableToXML loops through an array of allocatables, fetches the minimum required to build sql queries. |
|---|
| 228 | * |
|---|
| 229 | */ |
|---|
| 230 | public void allocatableToLimitedXML(Allocatable[] alls, boolean isPerson, java.io.PrintWriter out, Appointment appointment) { |
|---|
| 231 | String blockName = (isPerson)? "person" : "resource"; |
|---|
| 232 | if (alls.length > 0) { |
|---|
| 233 | out.println(" <" + blockName + "s>"); |
|---|
| 234 | for (int j=0;j<alls.length;j++) { |
|---|
| 235 | boolean hasAlloc=appointment.getReservation().hasAllocated(alls[j],appointment); |
|---|
| 236 | if(hasAlloc) |
|---|
| 237 | { |
|---|
| 238 | Classification classification = alls[j].getClassification(); |
|---|
| 239 | String allKey = classification.getType().getElementKey(); |
|---|
| 240 | String allocatableId = ((RefEntity) alls[j]).getId().toString(); |
|---|
| 241 | if ( allocatableId.contains("_") == true) { |
|---|
| 242 | allocatableId = allocatableId.split("_")[1]; |
|---|
| 243 | } |
|---|
| 244 | out.println(" <" + blockName + " typekey='" + allKey + "' relid='" + allocatableId + "' />"); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | out.println(" </" + blockName + "s>"); |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | } |
|---|