cmaclellan Posted February 27, 2007 Share Posted February 27, 2007 I am using Jdeveloper to design an application that needs to create dynamic reports depending on what the user selects. Is there a way that I can set the sql query in the report file then generate a report with that? Link to comment Share on other sites More sharing options...
Bhooms Posted February 28, 2007 Share Posted February 28, 2007 Its possible to generate the jasper report dynamically.you need to create the jasper design dynamically.Write a method and pass the resultset mata data and a empty jasper design(create a empty jrxml and call it) as a parameters. JRDesignStaticText staticText = null; JRDesignField designField = null; JRDesignTextField textField = null; JRDesignExpression exp = null; JRBand columnHeader = design.getColumnHeader(); List columnHeaderList = (ArrayList) columnHeader.getChildren(); List designFieldList = (ArrayList) design.getMainDesignDataset() .getFieldsList(); Map designFieldMap = (HashMap) design.getMainDesignDataset() .getFieldsMap(); JRBand bandDetail = design.getDetail(); List bandDetailList = bandDetail.getChildren(); try { int xPos = 0; int columnCount = rsmd.getColumnCount(); int columnWidth = columnCount * 65 + 50; design.setColumnWidth(columnWidth); for (int j = 1; j <= columnCount; j++) { String userFriendlyMessage = null; String column = rsmd.getColumnName(j); //String columnType = rsmd.getColumnClassName(j); try{ userFriendlyMessage = resourcebundle.getString(column); } catch(MissingResourceException mre) { // if this exception is thrown then we know that the resource bundle does not contain key } if( userFriendlyMessage == null) { userFriendlyMessage = column; } userFriendlyMessage = userFriendlyMessage.trim(); //Setting the Header part of JRXML staticText = new JRDesignStaticText(); staticText.setText(userFriendlyMessage); staticText.setKey("staticText-" + j); staticText.setBold(true); staticText.setTopBorder(new Byte("1")); staticText.setBottomBorder(new Byte("1")); staticText.setLeftBorder(new Byte("1")); staticText.setRightBorder(new Byte("1")); staticText.setBottomBorderColor(new Color(0, 0, 0)); staticText.setTopBorderColor(new Color(0, 0, 0)); staticText.setLeftBorderColor(new Color(0, 0, 0)); staticText.setRightBorderColor(new Color(0, 0, 0)); staticText.setForecolor(new Color(255,255,255)); staticText.setBackcolor(new Color(0, 102, 255)); staticText.setMode(JRDesignStaticText.MODE_OPAQUE); staticText.setPdfFontName("Helvetica-Bold"); staticText.setFontName("SanSerif"); staticText.setFontSize(10); staticText.setWidth(65); staticText.setHeight(30); staticText.setX(xPos); staticText.setY(0); staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER); columnHeaderList.add(staticText); //Adding the Design field to JRXML designField = new JRDesignField(); designField.setName(column); designField.setValueClassName(rsmd.getColumnClassName(j).toString()); //designField.setValueClassName("java.lang.String"); designFieldList.add(designField); designFieldMap.put(column, designField); //Adding the Detail Part to JRXML textField = new JRDesignTextField(); exp = new JRDesignExpression(); exp.setValueClassName(rsmd.getColumnClassName(j).toString()); exp.setText("$F{" + column + "}");// $F{CYCLE_NUM} textField.setKey("textField-" + j); textField.setBlankWhenNull(true); textField.setTopBorder(new Byte("1")); textField.setBottomBorder(new Byte("1")); textField.setLeftBorder(new Byte("1")); textField.setRightBorder(new Byte("1")); textField.setBold(true); textField.setHeight(19); textField.setWidth(65); textField.setX(xPos); textField.setY(0); if (rsmd.getColumnClassName(j).equalsIgnoreCase( "java.math.BigDecimal")) { textField .setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT); } else { textField .setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER); } textField.setExpression(exp); bandDetailList.add(textField); xPos = xPos + 65; staticText = null; designField = null; textField = null; exp = null; }Hope this helps you. Link to comment Share on other sites More sharing options...
amir.ghaffarigc.com.pk Posted February 28, 2007 Share Posted February 28, 2007 Is it possible to create some basic design containing some basic elements/fields and using your approach modify template (change fields, change fields properties)? Link to comment Share on other sites More sharing options...
Bhooms Posted March 1, 2007 Share Posted March 1, 2007 Yes you can do thatAll you need to do design the jrxml and pass the jasper object to the method i mentioned. Link to comment Share on other sites More sharing options...
cmaclellan Posted March 6, 2007 Author Share Posted March 6, 2007 i finally got a chance to sit and read through you responce. it looks good. would this same method work with subreports? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now