org.openxava.actions
Class SimpleHTMLReportAction
java.lang.Object
org.openxava.actions.BaseAction
org.openxava.actions.ViewBaseAction
org.openxava.actions.SimpleHTMLReportAction
- All Implemented Interfaces:
- IAction, IChangeModeAction, IForwardAction, IModelAction, IModuleContextAction, IRequestAction
public class SimpleHTMLReportAction
- extends ViewBaseAction
- implements IForwardAction, IModelAction
To create reports from simple html templates.
How to use the SimpleHtmlReportAction
Process
Create an action which extends SimpleHtmlReportAction.
Create a report template
Add the action in controllers.xml,
Action
Create an action which extends SimpleHtmlReportAction.
If your entity does not contain collections, you don't have to create
this action, you can use SimpleHtmlReportAction
public class ReportProjectAction extends SimpleHtmlReportAction {
public Map<String, Object> getParameters() throws Exception {
Project p = (Project)MapFacade.findEntity(getModelName(), getView().getKeyValuesWithValue());
return getParameters(p);
}
public static Map<String, Object> getParameters(Project p) throws Exception {
Map<String, Object> parameters = new HashMap<String, Object>();
// get all the field contents of the entity
parameters.putAll(getEntityParameters(p));
// get the field contents of the collections
parameters.put("milestones", getCollectionParametersList(p.getMilestones()));
parameters.put("actions", getCollectionParametersList(p.getActions()));
return parameters;
}
}
In most cases, you just have to call
getEntityParameters(your_entity) and
getCollectionParametersList(your_collection) in the action.
By defining a static function to get the parameters, you can reuse the
code for an extension of ReportProjectAction.
Report
Create a report template in /reports (if this folder does not exist,
create it as a source folder). You can create it with any WYSIWYG
editor (SeaMonkey is a good free one).
This template should be called entity_name.html, but you can create
others as you want. The (very simple) syntax for the report is
explained at the end.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Project - ${reference}</title>
<link href="/OpenXavaTest/xava/style/report.css" rel="stylesheet" type="text/css">
</head>
<body>
<table>
<tr><td>Name:</td><td>${name}</td></tr>
<tr><td>Reference:</td><td>${reference}</td></tr>
<tr><td>Owner:</td><td>${owner.firstName} ${owner.lastName}</td></tr>
<!-- $$if(customers) -->
<tr><td>Customers:</td><td>${customer}</td></tr>
<!-- $$endif(customers) -->
</table>
<table>
<tr>
<td>Milestone</td>
<td>Target</td>
<td>Achieved</td>
</tr>
<!-- $$for(milestones) -->
<tr>
<td>${milestone.name}</td>
<td>${targetDate}</td>
<td>${achievedDate}</td>
</tr>
<!-- $$endfor(milestones) -->
</table>
</body>
</html>
controllers.xml
<controller name="Project">
<extends controller="Typical"/>
<action name="datasheet" image="images/report.gif" mode="detail"
class="org.openxava.actions.ReportProjectAction" >
<set property="template" value="/Project.html" />
</action>
<action name="actionsReport" image="images/report.gif" mode="detail"
class="org.openxava.actions.ReportProjectAction" >
<set property="template" value="/ProjectActions.html" />
</action>
</controller>
<controller name="Company">
<extends controller="Typical"/>
<action name="report" image="images/report.gif" mode="detail"
class="org.openxava.actions.SimpleHtmlReportAction" />
</controller>
<controller name="SimpleHtmlReport">
<action name="report" image="images/report.gif" mode="detail"
class="org.openxava.actions.SimpleHtmlReportAction" />
</controller>
Here we have used twice the same report action with 2 different
templates for Project to report on different part of the entity.
We also can use the SimpleHtmlReportAction if we don't want to display
collections in the report, such as described in the Company controller.
Developping reports very fast
1. In application.xml, add the SimpleHtmlReport controller to the
module you want to report on.
2. In /reports, create a report named entity_name.html, open it in
Eclipse and just write ${fields} inside, save
3. Start Tomcat, launch your browser, select your module and click on
Report, a report will be generated with all the available fields
4. Save this report under /reports/entity_name.html
5. Done! If you refresh the reports folder in Eclipse and wait a little
bit, when you click on report in the browser you will see a complete
report of your entity (without the collections)
Syntax for the template
Field names
${field_name} is replaced in the template by the value contained in the
Map returned by getParameters()
field_name can contain references such as ${parent.child.name} with a
default (adjustable) depth of 5.
Control
There are 3 types of control blocks
if
Syntax: <!-- $$if(field_name) -->Some text which can contain
control blocks<!-- $$endif(field_name) -->
The content of the block will only appear if field_name IS in the
getParameters() Map and is not empty.
ifnot
Syntax: <!-- $$ifnot(field_name) -->Some text which can contain
control blocks<!-- $$endifnot(field_name) -->
The content of the block will only appear if field_name is NOT in the
getParameters() Map or is empty.
for
Syntax: <!-- $$for(field_name) -->Some text which can contain
control blocks<!-- $$endfor(field_name) -->
The content of the block will be repeated as many times as there are
items in the Vector<Map<String, Object>> returned by
getParameters().get(field_name)
- Author:
- Laurent Wibaux
|
Method Summary |
void |
execute()
|
protected java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> |
getCollectionParametersList(java.util.Collection<?> collection)
Get a list of maps of [name, value] for each one of the objects in the passed collection |
java.lang.String |
getCollectionTable(java.lang.Class<?> collectionEntityClass,
java.lang.String collectionName)
|
java.lang.String |
getCollectionTable(java.lang.Class<?> collectionEntityClass,
java.lang.String collectionName,
int maxDepth)
|
java.lang.String |
getDepth()
|
protected java.util.Map<java.lang.String,java.lang.String> |
getEntityParameters(java.lang.Object entity)
Get a map of [name, value] for the passed object |
java.lang.String |
getFieldsTable(java.lang.Object entity,
int depth)
|
java.lang.String |
getForwardURI()
The URI to go. |
protected java.util.Map<java.lang.String,java.lang.Object> |
getParameters()
|
java.lang.String |
getTemplate()
|
java.lang.String |
getValuesTable(java.lang.Object entity,
int depth)
|
boolean |
inNewWindow()
|
void |
setDepth(java.lang.String depth)
|
void |
setModel(java.lang.String modelName)
|
void |
setTemplate(java.lang.String template)
|
| Methods inherited from class org.openxava.actions.ViewBaseAction |
closeDialog, getModelName, getPreviousView, getPreviousViews, getView, resetDescriptionsCache, returnToPreviousView, setControllers, setPreviousViews, setView, showDialog, showDialog, showNewView, showView |
| Methods inherited from class org.openxava.actions.BaseAction |
addActions, addError, addErrors, addInfo, addMessage, addMessages, addWarning, clearActions, executeAfter, executeBefore, getContext, getEnvironment, getErrors, getLocale, getManager, getMessages, getNextMode, getRequest, removeActions, returnToPreviousControllers, setContext, setDefaultControllers, setEnvironment, setErrors, setMessages, setNextMode, setRequest |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
SimpleHTMLReportAction
public SimpleHTMLReportAction()
execute
public void execute()
throws java.lang.Exception
- Specified by:
execute in interface IAction
- Throws:
java.lang.Exception
getParameters
protected java.util.Map<java.lang.String,java.lang.Object> getParameters()
throws java.lang.Exception
- Throws:
java.lang.Exception
getForwardURI
public java.lang.String getForwardURI()
- Description copied from interface:
IForwardAction
- The URI to go.
If it starts with "http://" or "http://" the action will forward to the
absolute URL in internet (since v4m1), if it starts with "javascript:" the
corresponding code will executed by the browser (since 4.0.1),
otherwise it will forward to a resource inside this application.
- Specified by:
getForwardURI in interface IForwardAction
inNewWindow
public boolean inNewWindow()
- Specified by:
inNewWindow in interface IForwardAction
setModel
public void setModel(java.lang.String modelName)
- Specified by:
setModel in interface IModelAction
getFieldsTable
public java.lang.String getFieldsTable(java.lang.Object entity,
int depth)
throws java.lang.Exception
- Throws:
java.lang.Exception
getValuesTable
public java.lang.String getValuesTable(java.lang.Object entity,
int depth)
throws java.lang.Exception
- Throws:
java.lang.Exception
getCollectionTable
public java.lang.String getCollectionTable(java.lang.Class<?> collectionEntityClass,
java.lang.String collectionName)
throws java.lang.Exception
- Throws:
java.lang.Exception
getCollectionTable
public java.lang.String getCollectionTable(java.lang.Class<?> collectionEntityClass,
java.lang.String collectionName,
int maxDepth)
throws java.lang.Exception
- Throws:
java.lang.Exception
getTemplate
public java.lang.String getTemplate()
setTemplate
public void setTemplate(java.lang.String template)
setDepth
public void setDepth(java.lang.String depth)
getDepth
public java.lang.String getDepth()
getEntityParameters
protected java.util.Map<java.lang.String,java.lang.String> getEntityParameters(java.lang.Object entity)
throws java.lang.Exception
- Get a map of [name, value] for the passed object
- Parameters:
entity - - the Object to parsemaxDepth - - the maximum depth while parsing the ManyToOne fields
- Returns:
- the map of field names and values
- Throws:
java.lang.Exception
getCollectionParametersList
protected java.util.Collection<java.util.Map<java.lang.String,java.lang.String>> getCollectionParametersList(java.util.Collection<?> collection)
throws java.lang.Exception
- Get a list of maps of [name, value] for each one of the objects in the passed collection
- Parameters:
collection - - the Collection to parse
- Returns:
- the list of maps
- Throws:
java.lang.Exception