JSF HTTP 500 error

Reply

Join Date: Sep 2007
Posts: 23
Reputation: kohuke is an unknown quantity at this point 
Solved Threads: 0
kohuke kohuke is offline Offline
Newbie Poster

JSF HTTP 500 error

 
0
  #1
Aug 7th, 2008
Hi again (I'm annoying i know, but i just get stuck with things).
I'm using this tutorial: http://www.netbeans.org/kb/61/web/js...ud-wizard.html
I'm using it in combination with mysql+tomcat not java database+classfish.
I have imported the following libraries (after googling what is needed for persistance in tomcat and mysql):
JSF 1.1
JSF 1.2
JTA
JSF 1.1/1.2 Support
MySql JDBC Driver
TopLink Essentials
WebUIComponenets
WebUi Default theme an the usual libraries which is added automatically in netbeans.

I use the automatic generation of the java class files. and my problem is that the main page
where it displays the links is just fine.
Welcome.JSF:
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3.  
  4. <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
  5. <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  8. "http://www.w3.org/TR/html4/loose.dtd">
  9.  
  10. <%--
  11. This file is an entry point for JavaServer Faces application.
  12. --%>
  13.  
  14. <html>
  15. <head>
  16. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  17. <title>JSP Page</title>
  18. </head>
  19. <body>
  20. <f:view>
  21. <h:form>
  22. <h1><h:outputText value="JavaServer Faces" /></h1>
  23. <br/>
  24. <h:commandLink action="#{propertyType.listSetup}" value="Show All PropertyType Items"/>
  25.  
  26. <br/>
  27. <h:commandLink action="#{propertyClass.listSetup}" value="Show All PropertyClass Items"/>
  28.  
  29. <br/>
  30. <h:commandLink action="#{property.listSetup}" value="Show All Property Items"/>
  31.  
  32. <br/>
  33. <h:commandLink action="#{employee.listSetup}" value="Show All Employee Items"/>
  34. </h:form>
  35.  
  36. </f:view>
  37. </body>
  38. </html>

But as soon as i click on any of the links i get:
  1. HTTP Status 500 -
  2.  
  3. --------------------------------------------------------------------------------
  4.  
  5. type Status report
  6.  
  7. message
  8.  
  9. description The server encountered an internal error () that prevented it from fulfilling this request.
  10.  
  11.  
  12. --------------------------------------------------------------------------------
  13.  
  14. Apache Tomcat/6.0.16
this error.

Also i see some errors in catalin.log files:
  1. 7.08.2008 9:13:29 org.apache.catalina.core.AprLifecycleListener init
  2. INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_06\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\Program Files\ZipGenius 6\;C:\Program Files\Rational\common;C:\Program Files\WinSCP\;C:\Program Files\Intel\DMIX;C:\Program Files\MySQL\MySQL Server 5.0\bin
  3. 7.08.2008 9:13:29 org.apache.coyote.http11.Http11Protocol init
  4. INFO: Initializing Coyote HTTP/1.1 on http-8084
  5. 7.08.2008 9:13:29 org.apache.catalina.startup.Catalina load
  6. INFO: Initialization processed in 415 ms
  7. 7.08.2008 9:13:29 org.apache.catalina.core.StandardService start
  8. INFO: Starting service Catalina
  9. 7.08.2008 9:13:29 org.apache.catalina.core.StandardEngine start
  10. INFO: Starting Servlet Engine: Apache Tomcat/6.0.16
  11. 7.08.2008 9:13:30 org.apache.coyote.http11.Http11Protocol start
  12. INFO: Starting Coyote HTTP/1.1 on http-8084
  13. 7.08.2008 9:13:30 org.apache.jk.common.ChannelSocket init
  14. INFO: JK: ajp13 listening on /0.0.0.0:8009
  15. 7.08.2008 9:13:30 org.apache.jk.server.JkMain start
  16. INFO: Jk running ID=0 time=0/16 config=null
  17. 7.08.2008 9:13:30 org.apache.catalina.startup.Catalina start
  18. INFO: Server startup in 496 ms
  19. 7.08.2008 9:13:30 com.sun.faces.config.ConfigureListener contextInitialized
  20. INFO: Initializing Sun's JavaServer Faces implementation (1.2_04-b20-p03) for context '/Varad2'
  21. 7.08.2008 9:13:32 org.apache.catalina.core.StandardContext start
  22. INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Varad2] has already been started
  23. 7.08.2008 9:13:35 com.sun.faces.application.ApplicationAssociate createAndMaybeStoreManagedBeans
  24. SEVERE: JSF1001: Managedbean propertyClass could not be created.
  25. 7.08.2008 9:13:35 com.sun.faces.lifecycle.LifecycleImpl phase
  26. WARNING: executePhase(RENDER_RESPONSE 6,com.sun.faces.context.FacesContextImpl@b2fb1e) threw exception
  27. com.sun.rave.web.ui.appbase.ApplicationException: javax.faces.FacesException: javax.naming.NameNotFoundException: Name entities.PropertyClassController is not bound in this Context
  28. at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.cleanup(ViewHandlerImpl.java:594)
  29. at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.renderView(ViewHandlerImpl.java:311)
  30. at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
  31. at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
  32. at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
  33. at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
  34. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
  35. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  36. at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
  37. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
  38. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  39. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
  40. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  41. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
  42. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
  43. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  44. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
  45. at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
  46. at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
  47. at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
  48. at java.lang.Thread.run(Thread.java:619)
  49. Caused by: javax.faces.FacesException: javax.faces.FacesException: javax.naming.NameNotFoundException: Name entities.PropertyClassController is not bound in this Context
  50. at com.sun.faces.application.ApplicationAssociate.createAndMaybeStoreManagedBeans(ApplicationAssociate.java:537)
  51. at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:82)
  52. at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
  53. at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:64)
  54. at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:61)
  55. at org.apache.el.parser.AstValue.getTarget(AstValue.java:59)
  56. at org.apache.el.parser.AstValue.invoke(AstValue.java:148)
  57. at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
  58. at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
  59. at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
  60. at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
  61. at com.sun.rave.web.ui.appbase.faces.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
  62. at javax.faces.component.UICommand.broadcast(UICommand.java:383)
  63. at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:447)
  64. at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:752)
  65. at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
  66. at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
  67. at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
  68. at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
  69. ... 15 more
  70. Caused by: javax.faces.FacesException: javax.naming.NameNotFoundException: Name entities.PropertyClassController is not bound in this Context
  71. at com.sun.faces.config.ManagedBeanFactoryImpl.newInstance(ManagedBeanFactoryImpl.java:340)
  72. at com.sun.faces.application.ApplicationAssociate.createAndMaybeStoreManagedBeans(ApplicationAssociate.java:527)
  73. ... 33 more
  74. Caused by: javax.naming.NameNotFoundException: Name entities.PropertyClassController is not bound in this Context
  75. at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
  76. at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
  77. at org.apache.catalina.util.DefaultAnnotationProcessor.lookupFieldResource(DefaultAnnotationProcessor.java:203)
  78. at org.apache.catalina.util.DefaultAnnotationProcessor.processAnnotations(DefaultAnnotationProcessor.java:135)
  79. at com.sun.faces.vendor.Tomcat6InjectionProvider.inject(Tomcat6InjectionProvider.java:46)
  80. at com.sun.faces.config.ManagedBeanFactoryImpl.newInstance(ManagedBeanFactoryImpl.java:298)
  81. ... 34 more
  82. 7.08.2008 9:13:35 com.sun.faces.lifecycle.LifecycleImpl phase
  83. WARNING: phase(RENDER_RESPONSE 6,com.sun.faces.context.FacesContextImpl@b2fb1e) threw exception: com.sun.rave.web.ui.appbase.ApplicationException: javax.faces.FacesException: javax.naming.NameNotFoundException: Name entities.PropertyClassController is not bound in this Context javax.faces.FacesException: javax.naming.NameNotFoundException: Name entities.PropertyClassController is not bound in this Context
  84. com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.cleanup(ViewHandlerImpl.java:594)
  85. com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.afterPhase(ViewHandlerImpl.java:470)
  86. com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:280)
  87. com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
  88. javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
  89. org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
  90. org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  91. org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
  92. org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
  93. org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  94. org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
  95. org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  96. org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
  97. org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
  98. org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  99. org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
  100. org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
  101. org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
  102. org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
  103. java.lang.Thread.run(Thread.java:619)
  104.  
Can some please explain what there error mean. I think those are the problems which cause the page not to show stuff.

The source code itself (atleast for one object - property) is as follows:
Detail.jsp:
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  4. <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. <title>Property Detail</title>
  9. </head>
  10. <body>
  11. <f:view>
  12. <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
  13. <h1>Property Detail</h1>
  14. <h:form>
  15. <h:panelGrid columns="2">
  16. <h:outputText value="Property:"/>
  17. <h:outputText value="#{property.property.property}" title="Property" />
  18. <h:outputText value="Name:"/>
  19. <h:outputText value="#{property.property.name}" title="Name" />
  20. <h:outputText value="Created:"/>
  21. <h:outputText value="#{property.property.created}" title="Created" >
  22. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  23. </h:outputText>
  24. <h:outputText value="Updated:"/>
  25. <h:outputText value="#{property.property.updated}" title="Updated" >
  26. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  27. </h:outputText>
  28. <h:outputText value="CreatedBy:"/>
  29. <h:panelGroup>
  30. <h:outputText value=" #{property.property.createdBy}"/>
  31. <h:panelGroup rendered="#{property.property.createdBy != null}">
  32. <h:outputText value=" ("/>
  33. <h:commandLink value="Show" action="#{employee.detailSetup}">
  34. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  35. <f:param name="jsfcrud.currentEmployee" value="#{employee.asString[property.property.createdBy]}"/>
  36. <f:param name="jsfcrud.relatedController" value="property"/>
  37. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  38. </h:commandLink>
  39. <h:outputText value=" "/>
  40. <h:commandLink value="Edit" action="#{employee.editSetup}">
  41. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  42. <f:param name="jsfcrud.currentEmployee" value="#{employee.asString[property.property.createdBy]}"/>
  43. <f:param name="jsfcrud.relatedController" value="property"/>
  44. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  45. </h:commandLink>
  46. <h:outputText value=" "/>
  47. <h:commandLink value="Destroy" action="#{employee.destroy}">
  48. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  49. <f:param name="jsfcrud.currentEmployee" value="#{employee.asString[property.property.createdBy]}"/>
  50. <f:param name="jsfcrud.relatedController" value="property"/>
  51. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  52. </h:commandLink>
  53. <h:outputText value=" )"/>
  54. </h:panelGroup>
  55. </h:panelGroup>
  56. <h:outputText value="PropertyType:"/>
  57. <h:panelGroup>
  58. <h:outputText value=" #{property.property.propertyType}"/>
  59. <h:panelGroup rendered="#{property.property.propertyType != null}">
  60. <h:outputText value=" ("/>
  61. <h:commandLink value="Show" action="#{propertyType.detailSetup}">
  62. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  63. <f:param name="jsfcrud.currentPropertyType" value="#{propertyType.asString[property.property.propertyType]}"/>
  64. <f:param name="jsfcrud.relatedController" value="property"/>
  65. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  66. </h:commandLink>
  67. <h:outputText value=" "/>
  68. <h:commandLink value="Edit" action="#{propertyType.editSetup}">
  69. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  70. <f:param name="jsfcrud.currentPropertyType" value="#{propertyType.asString[property.property.propertyType]}"/>
  71. <f:param name="jsfcrud.relatedController" value="property"/>
  72. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  73. </h:commandLink>
  74. <h:outputText value=" "/>
  75. <h:commandLink value="Destroy" action="#{propertyType.destroy}">
  76. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  77. <f:param name="jsfcrud.currentPropertyType" value="#{propertyType.asString[property.property.propertyType]}"/>
  78. <f:param name="jsfcrud.relatedController" value="property"/>
  79. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  80. </h:commandLink>
  81. <h:outputText value=" )"/>
  82. </h:panelGroup>
  83. </h:panelGroup>
  84. <h:outputText value="UpdatedBy:"/>
  85. <h:panelGroup>
  86. <h:outputText value=" #{property.property.updatedBy}"/>
  87. <h:panelGroup rendered="#{property.property.updatedBy != null}">
  88. <h:outputText value=" ("/>
  89. <h:commandLink value="Show" action="#{employee.detailSetup}">
  90. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  91. <f:param name="jsfcrud.currentEmployee" value="#{employee.asString[property.property.updatedBy]}"/>
  92. <f:param name="jsfcrud.relatedController" value="property"/>
  93. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  94. </h:commandLink>
  95. <h:outputText value=" "/>
  96. <h:commandLink value="Edit" action="#{employee.editSetup}">
  97. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  98. <f:param name="jsfcrud.currentEmployee" value="#{employee.asString[property.property.updatedBy]}"/>
  99. <f:param name="jsfcrud.relatedController" value="property"/>
  100. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  101. </h:commandLink>
  102. <h:outputText value=" "/>
  103. <h:commandLink value="Destroy" action="#{employee.destroy}">
  104. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  105. <f:param name="jsfcrud.currentEmployee" value="#{employee.asString[property.property.updatedBy]}"/>
  106. <f:param name="jsfcrud.relatedController" value="property"/>
  107. <f:param name="jsfcrud.relatedControllerType" value="entities.PropertyController"/>
  108. </h:commandLink>
  109. <h:outputText value=" )"/>
  110. </h:panelGroup>
  111. </h:panelGroup>
  112. </h:panelGrid>
  113. <br />
  114. <h:commandLink action="#{property.destroy}" value="Destroy">
  115. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}" />
  116. </h:commandLink>
  117. <br />
  118. <br />
  119. <h:commandLink action="#{property.editSetup}" value="Edit">
  120. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}" />
  121. </h:commandLink>
  122. <br />
  123. <h:commandLink action="#{property.createSetup}" value="New Property" />
  124. <br />
  125. <h:commandLink action="#{property.listSetup}" value="Show All Property Items"/>
  126. <br />
  127. <a href="/Varad2/faces/welcomeJSF.jsp">Index</a>
  128. </h:form>
  129. </f:view>
  130. </body>
  131. </html>
Edit.jsp:
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  4. <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. <title>Editing Property</title>
  9. </head>
  10. <body>
  11. <f:view>
  12. <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
  13. <h1>Editing Property</h1>
  14. <h:form>
  15. <h:panelGrid columns="2">
  16. <h:outputText value="Property:"/>
  17. <h:outputText value="#{property.property.property}" title="Property" />
  18. <h:outputText value="Name:"/>
  19. <h:inputText id="name" value="#{property.property.name}" title="Name" />
  20. <h:outputText value="Created (MM/dd/yyyy HH:mm:ss):"/>
  21. <h:inputText id="created" value="#{property.property.created}" title="Created" required="true" requiredMessage="The created field is required." >
  22. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  23. </h:inputText>
  24. <h:outputText value="Updated (MM/dd/yyyy HH:mm:ss):"/>
  25. <h:inputText id="updated" value="#{property.property.updated}" title="Updated" required="true" requiredMessage="The updated field is required." >
  26. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  27. </h:inputText>
  28. <h:outputText value="CreatedBy:"/>
  29. <h:selectOneMenu id="createdBy" value="#{property.property.createdBy}" title="CreatedBy" required="true" requiredMessage="The createdBy field is required." >
  30. <f:selectItems value="#{employee.employeesAvailableSelectOne}"/>
  31. </h:selectOneMenu>
  32. <h:outputText value="PropertyType:"/>
  33. <h:selectOneMenu id="propertyType" value="#{property.property.propertyType}" title="PropertyType" required="true" requiredMessage="The propertyType field is required." >
  34. <f:selectItems value="#{propertyType.propertyTypesAvailableSelectOne}"/>
  35. </h:selectOneMenu>
  36. <h:outputText value="UpdatedBy:"/>
  37. <h:selectOneMenu id="updatedBy" value="#{property.property.updatedBy}" title="UpdatedBy" >
  38. <f:selectItems value="#{employee.employeesAvailableSelectOne}"/>
  39. </h:selectOneMenu>
  40. </h:panelGrid>
  41. <br />
  42. <h:commandLink action="#{property.edit}" value="Save">
  43. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  44. </h:commandLink>
  45. <br />
  46. <br />
  47. <h:commandLink action="#{property.detailSetup}" value="Show" immediate="true">
  48. <f:param name="jsfcrud.currentProperty" value="#{property.asString[property.property]}"/>
  49. </h:commandLink>
  50. <br />
  51. <h:commandLink action="#{property.listSetup}" value="Show All Property Items" immediate="true"/>
  52. <br />
  53. <a href="/Varad2/faces/welcomeJSF.jsp">Index</a>
  54. </h:form>
  55. </f:view>
  56. </body>
  57. </html>

List.jsp:
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  4. <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. <title>Listing Property Items</title>
  9. </head>
  10. <body>
  11. <f:view>
  12. <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
  13. <h1>Listing Property Items</h1>
  14. <h:form>
  15. <h:outputText escape="false" value="(No Property Items Found)<br />" rendered="#{property.itemCount == 0}" />
  16. <h:panelGroup rendered="#{property.itemCount > 0}">
  17. <h:outputText value="Item #{property.firstItem + 1}..#{property.lastItem} of #{property.itemCount}"/>&nbsp;
  18. <h:commandLink action="#{property.prev}" value="Previous #{property.batchSize}" rendered="#{property.firstItem >= property.batchSize}"/>&nbsp;
  19. <h:commandLink action="#{property.next}" value="Next #{property.batchSize}" rendered="#{property.lastItem + property.batchSize <= property.itemCount}"/>&nbsp;
  20. <h:commandLink action="#{property.next}" value="Remaining #{property.itemCount - property.lastItem}"
  21. rendered="#{property.lastItem < property.itemCount && property.lastItem + property.batchSize > property.itemCount}"/>
  22. <h:dataTable value='#{property.propertys}' var='item' border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_oddrow,jsfcrud_evenrow" rules="all" style="border:solid 1px">
  23. <h:column>
  24. <f:facet name="header">
  25. <h:outputText value="Property"/>
  26. </f:facet>
  27. <h:outputText value=" #{item.property}"/>
  28. </h:column>
  29. <h:column>
  30. <f:facet name="header">
  31. <h:outputText value="Name"/>
  32. </f:facet>
  33. <h:outputText value=" #{item.name}"/>
  34. </h:column>
  35. <h:column>
  36. <f:facet name="header">
  37. <h:outputText value="Created"/>
  38. </f:facet>
  39. <h:outputText value="#{item.created}">
  40. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  41. </h:outputText>
  42. </h:column>
  43. <h:column>
  44. <f:facet name="header">
  45. <h:outputText value="Updated"/>
  46. </f:facet>
  47. <h:outputText value="#{item.updated}">
  48. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  49. </h:outputText>
  50. </h:column>
  51. <h:column>
  52. <f:facet name="header">
  53. <h:outputText value="CreatedBy"/>
  54. </f:facet>
  55. <h:outputText value=" #{item.createdBy}"/>
  56. </h:column>
  57. <h:column>
  58. <f:facet name="header">
  59. <h:outputText value="PropertyType"/>
  60. </f:facet>
  61. <h:outputText value=" #{item.propertyType}"/>
  62. </h:column>
  63. <h:column>
  64. <f:facet name="header">
  65. <h:outputText value="UpdatedBy"/>
  66. </f:facet>
  67. <h:outputText value=" #{item.updatedBy}"/>
  68. </h:column>
  69. <h:column>
  70. <f:facet name="header">
  71. <h:outputText escape="false" value="&nbsp;"/>
  72. </f:facet>
  73. <h:commandLink value="Show" action="#{property.detailSetup}">
  74. <f:param name="jsfcrud.currentProperty" value="#{property.asString[item]}"/>
  75. </h:commandLink>
  76. <h:outputText value=" "/>
  77. <h:commandLink value="Edit" action="#{property.editSetup}">
  78. <f:param name="jsfcrud.currentProperty" value="#{property.asString[item]}"/>
  79. </h:commandLink>
  80. <h:outputText value=" "/>
  81. <h:commandLink value="Destroy" action="#{property.destroy}">
  82. <f:param name="jsfcrud.currentProperty" value="#{property.asString[item]}"/>
  83. </h:commandLink>
  84. </h:column>
  85. </h:dataTable>
  86. </h:panelGroup>
  87. <br />
  88. <h:commandLink action="#{property.createSetup}" value="New Property"/>
  89. <br />
  90. <a href="/Varad2/faces/welcomeJSF.jsp">Index</a>
  91.  
  92. </h:form>
  93. </f:view>
  94. </body>
  95. </html>

new.jsp:
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3. <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
  4. <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. <title>New Property</title>
  9. </head>
  10. <body>
  11. <f:view>
  12. <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
  13. <h1>New Property</h1>
  14. <h:form>
  15. <h:inputHidden id="validateCreateField" validator="#{property.validateCreate}" value="value"/>
  16. <h:panelGrid columns="2">
  17. <h:outputText value="Name:"/>
  18. <h:inputText id="name" value="#{property.property.name}" title="Name" />
  19. <h:outputText value="Created (MM/dd/yyyy HH:mm:ss):"/>
  20. <h:inputText id="created" value="#{property.property.created}" title="Created" required="true" requiredMessage="The created field is required." >
  21. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  22. </h:inputText>
  23. <h:outputText value="Updated (MM/dd/yyyy HH:mm:ss):"/>
  24. <h:inputText id="updated" value="#{property.property.updated}" title="Updated" required="true" requiredMessage="The updated field is required." >
  25. <f:convertDateTime type="TIMESTAMP" pattern="MM/dd/yyyy HH:mm:ss" />
  26. </h:inputText>
  27. <h:outputText value="CreatedBy:"/>
  28. <h:selectOneMenu id="createdBy" value="#{property.property.createdBy}" title="CreatedBy" required="true" requiredMessage="The createdBy field is required." >
  29. <f:selectItems value="#{employee.employeesAvailableSelectOne}"/>
  30. </h:selectOneMenu>
  31. <h:outputText value="PropertyType:"/>
  32. <h:selectOneMenu id="propertyType" value="#{property.property.propertyType}" title="PropertyType" required="true" requiredMessage="The propertyType field is required." >
  33. <f:selectItems value="#{propertyType.propertyTypesAvailableSelectOne}"/>
  34. </h:selectOneMenu>
  35. <h:outputText value="UpdatedBy:"/>
  36. <h:selectOneMenu id="updatedBy" value="#{property.property.updatedBy}" title="UpdatedBy" >
  37. <f:selectItems value="#{employee.employeesAvailableSelectOne}"/>
  38. </h:selectOneMenu>
  39. </h:panelGrid>
  40. <br />
  41. <h:commandLink action="#{property.create}" value="Create"/>
  42. <br />
  43. <br />
  44. <h:commandLink action="#{property.listSetup}" value="Show All Property Items" immediate="true"/>
  45. <br />
  46. <a href="/Varad2/faces/welcomeJSF.jsp">Index</a>
  47. </h:form>
  48. </f:view>
  49. </body>
  50. </html>

Property.java
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package entities;
  7.  
  8. import java.io.Serializable;
  9. import java.util.Date;
  10. import javax.persistence.Column;
  11. import javax.persistence.Entity;
  12. import javax.persistence.GeneratedValue;
  13. import javax.persistence.GenerationType;
  14. import javax.persistence.Id;
  15. import javax.persistence.JoinColumn;
  16. import javax.persistence.ManyToOne;
  17. import javax.persistence.NamedQueries;
  18. import javax.persistence.NamedQuery;
  19. import javax.persistence.Table;
  20. import javax.persistence.Temporal;
  21. import javax.persistence.TemporalType;
  22.  
  23. /**
  24.  *
  25.  * @author kohuke
  26.  */
  27. @Entity
  28. @Table(name = "property")
  29. @NamedQueries({@NamedQuery(name = "Property.findByProperty", query = "SELECT p FROM Property p WHERE p.property = :property"), @NamedQuery(name = "Property.findByName", query = "SELECT p FROM Property p WHERE p.name = :name"), @NamedQuery(name = "Property.findByCreated", query = "SELECT p FROM Property p WHERE p.created = :created"), @NamedQuery(name = "Property.findByUpdated", query = "SELECT p FROM Property p WHERE p.updated = :updated")})
  30. public class Property implements Serializable {
  31. private static final long serialVersionUID = 1L;
  32. @Id
  33. @GeneratedValue(strategy = GenerationType.IDENTITY)
  34. @Column(name = "property", nullable = false)
  35. private Integer property;
  36. @Column(name = "name")
  37. private String name;
  38. @Column(name = "created", nullable = false)
  39. @Temporal(TemporalType.TIMESTAMP)
  40. private Date created;
  41. @Column(name = "updated", nullable = false)
  42. @Temporal(TemporalType.TIMESTAMP)
  43. private Date updated;
  44. @JoinColumn(name = "created_by", referencedColumnName = "employee")
  45. @ManyToOne(optional = false)
  46. private Employee createdBy;
  47. @JoinColumn(name = "property_type", referencedColumnName = "property_type")
  48. @ManyToOne(optional = false)
  49. private PropertyType propertyType;
  50. @JoinColumn(name = "updated_by", referencedColumnName = "employee")
  51. @ManyToOne
  52. private Employee updatedBy;
  53.  
  54. public Property() {
  55. }
  56.  
  57. public Property(Integer property) {
  58. this.property = property;
  59. }
  60.  
  61. public Property(Integer property, Date created, Date updated) {
  62. this.property = property;
  63. this.created = created;
  64. this.updated = updated;
  65. }
  66.  
  67. public Integer getProperty() {
  68. return property;
  69. }
  70.  
  71. public void setProperty(Integer property) {
  72. this.property = property;
  73. }
  74.  
  75. public String getName() {
  76. return name;
  77. }
  78.  
  79. public void setName(String name) {
  80. this.name = name;
  81. }
  82.  
  83. public Date getCreated() {
  84. return created;
  85. }
  86.  
  87. public void setCreated(Date created) {
  88. this.created = created;
  89. }
  90.  
  91. public Date getUpdated() {
  92. return updated;
  93. }
  94.  
  95. public void setUpdated(Date updated) {
  96. this.updated = updated;
  97. }
  98.  
  99. public Employee getCreatedBy() {
  100. return createdBy;
  101. }
  102.  
  103. public void setCreatedBy(Employee createdBy) {
  104. this.createdBy = createdBy;
  105. }
  106.  
  107. public PropertyType getPropertyType() {
  108. return propertyType;
  109. }
  110.  
  111. public void setPropertyType(PropertyType propertyType) {
  112. this.propertyType = propertyType;
  113. }
  114.  
  115. public Employee getUpdatedBy() {
  116. return updatedBy;
  117. }
  118.  
  119. public void setUpdatedBy(Employee updatedBy) {
  120. this.updatedBy = updatedBy;
  121. }
  122.  
  123. @Override
  124. public int hashCode() {
  125. int hash = 0;
  126. hash += (property != null ? property.hashCode() : 0);
  127. return hash;
  128. }
  129.  
  130. @Override
  131. public boolean equals(Object object) {
  132. // TODO: Warning - this method won't work in the case the id fields are not set
  133. if (!(object instanceof Property)) {
  134. return false;
  135. }
  136. Property other = (Property) object;
  137. if ((this.property == null && other.property != null) || (this.property != null && !this.property.equals(other.property))) {
  138. return false;
  139. }
  140. return true;
  141. }
  142.  
  143. @Override
  144. public String toString() {
  145. return property + ","+name+","+created+","+updated+","+createdBy+","+propertyType+","+updatedBy;
  146. }
  147.  
  148. }

PropertyClass.java
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package entities;
  7.  
  8. import java.io.Serializable;
  9. import java.util.Collection;
  10. import javax.persistence.Column;
  11. import javax.persistence.Entity;
  12. import javax.persistence.Id;
  13. import javax.persistence.NamedQueries;
  14. import javax.persistence.NamedQuery;
  15. import javax.persistence.OneToMany;
  16. import javax.persistence.Table;
  17.  
  18. /**
  19.  *
  20.  * @author kohuke
  21.  */
  22. @Entity
  23. @Table(name = "property_class")
  24. @NamedQueries({@NamedQuery(name = "PropertyClass.findByPropertyClass", query = "SELECT p FROM PropertyClass p WHERE p.propertyClass = :propertyClass"), @NamedQuery(name = "PropertyClass.findByName", query = "SELECT p FROM PropertyClass p WHERE p.name = :name"), @NamedQuery(name = "PropertyClass.findByDescription", query = "SELECT p FROM PropertyClass p WHERE p.description = :description")})
  25. public class PropertyClass implements Serializable {
  26. private static final long serialVersionUID = 1L;
  27. @Id
  28. @Column(name = "property_class", nullable = false)
  29. private Integer propertyClass;
  30. @Column(name = "name")
  31. private String name;
  32. @Column(name = "description")
  33. private String description;
  34. @OneToMany(mappedBy = "propertyClass")
  35. private Collection<PropertyType> propertyTypeCollection;
  36.  
  37. public PropertyClass() {
  38. }
  39.  
  40. public PropertyClass(Integer propertyClass) {
  41. this.propertyClass = propertyClass;
  42. }
  43.  
  44. public Integer getPropertyClass() {
  45. return propertyClass;
  46. }
  47.  
  48. public void setPropertyClass(Integer propertyClass) {
  49. this.propertyClass = propertyClass;
  50. }
  51.  
  52. public String getName() {
  53. return name;
  54. }
  55.  
  56. public void setName(String name) {
  57. this.name = name;
  58. }
  59.  
  60. public String getDescription() {
  61. return description;
  62. }
  63.  
  64. public void setDescription(String description) {
  65. this.description = description;
  66. }
  67.  
  68. public Collection<PropertyType> getPropertyTypeCollection() {
  69. return propertyTypeCollection;
  70. }
  71.  
  72. public void setPropertyTypeCollection(Collection<PropertyType> propertyTypeCollection) {
  73. this.propertyTypeCollection = propertyTypeCollection;
  74. }
  75.  
  76. @Override
  77. public int hashCode() {
  78. int hash = 0;
  79. hash += (propertyClass != null ? propertyClass.hashCode() : 0);
  80. return hash;
  81. }
  82.  
  83. @Override
  84. public boolean equals(Object object) {
  85. // TODO: Warning - this method won't work in the case the id fields are not set
  86. if (!(object instanceof PropertyClass)) {
  87. return false;
  88. }
  89. PropertyClass other = (PropertyClass) object;
  90. if ((this.propertyClass == null && other.propertyClass != null) || (this.propertyClass != null && !this.propertyClass.equals(other.propertyClass))) {
  91. return false;
  92. }
  93. return true;
  94. }
  95.  
  96. @Override
  97. public String toString() {
  98. return "entities.PropertyClass[propertyClass=" + propertyClass + "]";
  99. }
  100.  
  101. }

PropertyClassController.java:
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package entities;
  7.  
  8. import java.util.List;
  9. import java.util.Map;
  10. import javax.annotation.Resource;
  11. import javax.faces.component.UIComponent;
  12. import javax.faces.context.FacesContext;
  13. import javax.faces.model.SelectItem;
  14. import javax.persistence.EntityManager;
  15. import javax.persistence.EntityManagerFactory;
  16. import javax.persistence.PersistenceUnit;
  17. import javax.persistence.Query;
  18. import javax.faces.application.FacesMessage;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Method;
  21. import javax.faces.FacesException;
  22. import java.util.HashMap;
  23. import javax.faces.validator.ValidatorException;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.Collection;
  27. import javax.transaction.UserTransaction;
  28.  
  29. /**
  30.  *
  31.  * @author kohuke
  32.  */
  33. public class PropertyClassController {
  34. private PropertyClass propertyClass = null;
  35. private List<PropertyClass> propertyClasss = null;
  36. @Resource
  37. private UserTransaction utx = null;
  38. @PersistenceUnit(unitName = "Varad2PU")
  39. private EntityManagerFactory emf = null;
  40.  
  41. public EntityManager getEntityManager() {
  42. return emf.createEntityManager();
  43. }
  44. public int batchSize = 5;
  45. private int firstItem = 0;
  46. private int itemCount = -1;
  47.  
  48. public void setPropertyTypeCollectionOfPropertyClass(PropertyType[] propertyTypeCollection) {
  49. List<PropertyType> propertyTypeCollectionList = Arrays.asList(propertyTypeCollection);
  50. propertyClass.setPropertyTypeCollection(propertyTypeCollectionList);
  51. }
  52.  
  53. public PropertyType[] getPropertyTypeCollectionOfPropertyClass() {
  54. Collection<PropertyType> propertyTypeCollection = propertyClass.getPropertyTypeCollection();
  55. if (propertyTypeCollection == null) {
  56. return new PropertyType[0];
  57. }
  58. return propertyTypeCollection.toArray(new PropertyType[0]);
  59. }
  60.  
  61. public SelectItem[] getPropertyClasssAvailableSelectMany() {
  62. return getPropertyClasssAvailable(false);
  63. }
  64.  
  65. public SelectItem[] getPropertyClasssAvailableSelectOne() {
  66. return getPropertyClasssAvailable(true);
  67. }
  68.  
  69. private SelectItem[] getPropertyClasssAvailable(boolean one) {
  70. List<PropertyClass> allPropertyClasss = getPropertyClasss(true);
  71. int size = one ? allPropertyClasss.size() + 1 : allPropertyClasss.size();
  72. SelectItem[] items = new SelectItem[size];
  73. int i = 0;
  74. if (one) {
  75. items[0] = new SelectItem("", "---");
  76. i++;
  77. }
  78. for (PropertyClass x : allPropertyClasss) {
  79. items[i++] = new SelectItem(x, x.toString());
  80. }
  81. return items;
  82. }
  83.  
  84. public PropertyClass getPropertyClass() {
  85. if (propertyClass == null) {
  86. propertyClass = getPropertyClassFromRequest();
  87. }
  88. if (propertyClass == null) {
  89. propertyClass = new PropertyClass();
  90. }
  91. return propertyClass;
  92. }
  93.  
  94. public String listSetup() {
  95. reset(true);
  96. return "propertyClass_list";
  97. }
  98.  
  99. public String createSetup() {
  100. reset(false);
  101. propertyClass = new PropertyClass();
  102. return "propertyClass_create";
  103. }
  104.  
  105. public String create() {
  106. if (propertyClass.getPropertyTypeCollection() == null) {
  107. propertyClass.setPropertyTypeCollection(new ArrayList<PropertyType>());
  108. }
  109. EntityManager em = getEntityManager();
  110. try {
  111. utx.begin();
  112. List<PropertyType> attachedPropertyTypeCollection = new ArrayList<PropertyType>();
  113. for (PropertyType propertyTypeCollectionPropertyTypeToAttach : propertyClass.getPropertyTypeCollection()) {
  114. propertyTypeCollectionPropertyTypeToAttach = em.getReference(propertyTypeCollectionPropertyTypeToAttach.getClass(), propertyTypeCollectionPropertyTypeToAttach.getPropertyType());
  115. attachedPropertyTypeCollection.add(propertyTypeCollectionPropertyTypeToAttach);
  116. }
  117. propertyClass.setPropertyTypeCollection(attachedPropertyTypeCollection);
  118. em.persist(propertyClass);
  119. for (PropertyType propertyTypeCollectionPropertyType : propertyClass.getPropertyTypeCollection()) {
  120. PropertyClass oldPropertyClassOfPropertyTypeCollectionPropertyType = propertyTypeCollectionPropertyType.getPropertyClass();
  121. propertyTypeCollectionPropertyType.setPropertyClass(propertyClass);
  122. propertyTypeCollectionPropertyType = em.merge(propertyTypeCollectionPropertyType);
  123. if (oldPropertyClassOfPropertyTypeCollectionPropertyType != null) {
  124. oldPropertyClassOfPropertyTypeCollectionPropertyType.getPropertyTypeCollection().remove(propertyTypeCollectionPropertyType);
  125. oldPropertyClassOfPropertyTypeCollectionPropertyType = em.merge(oldPropertyClassOfPropertyTypeCollectionPropertyType);
  126. }
  127. }
  128. utx.commit();
  129. addSuccessMessage("PropertyClass was successfully created.");
  130. } catch (Exception ex) {
  131. try {
  132. if (findPropertyClass(propertyClass.getPropertyClass()) != null) {
  133. addErrorMessage("PropertyClass " + propertyClass + " already exists.");
  134. } else {
  135. ensureAddErrorMessage(ex, "A persistence error occurred.");
  136. }
  137. utx.rollback();
  138. } catch (Exception e) {
  139. ensureAddErrorMessage(e, "An error occurred attempting to roll back the transaction.");
  140. }
  141. return null;
  142. } finally {
  143. em.close();
  144. }
  145. return listSetup();
  146. }
  147.  
  148. public String detailSetup() {
  149. return scalarSetup("propertyClass_detail");
  150. }
  151.  
  152. public String editSetup() {
  153. return scalarSetup("propertyClass_edit");
  154. }
  155.  
  156. private String scalarSetup(String destination) {
  157. reset(false);
  158. propertyClass = getPropertyClassFromRequest();
  159. if (propertyClass == null) {
  160. String requestPropertyClassString = getRequestParameter("jsfcrud.currentPropertyClass");
  161. addErrorMessage("The propertyClass with id " + requestPropertyClassString + " no longer exists.");
  162. String relatedControllerOutcome = relatedControllerOutcome();
  163. if (relatedControllerOutcome != null) {
  164. return relatedControllerOutcome;
  165. }
  166. return listSetup();
  167. }
  168. return destination;
  169. }
  170.  
  171. public String edit() {
  172. PropertyClassConverter converter = new PropertyClassConverter();
  173. String propertyClassString = converter.getAsString(FacesContext.getCurrentInstance(), null, propertyClass);
  174. String currentPropertyClassString = getRequestParameter("jsfcrud.currentPropertyClass");
  175. if (propertyClassString == null || propertyClassString.length() == 0 || !propertyClassString.equals(currentPropertyClassString)) {
  176. String outcome = editSetup();
  177. if ("propertyClass_edit".equals(outcome)) {
  178. addErrorMessage("Could not edit propertyClass. Try again.");
  179. }
  180. return outcome;
  181. }
  182. EntityManager em = getEntityManager();
  183. try {
  184. utx.begin();
  185. PropertyClass persistentPropertyClass = em.find(PropertyClass.class, propertyClass.getPropertyClass());
  186. Collection<PropertyType> propertyTypeCollectionOld = persistentPropertyClass.getPropertyTypeCollection();
  187. Collection<PropertyType> propertyTypeCollectionNew = propertyClass.getPropertyTypeCollection();
  188. List<PropertyType> attachedPropertyTypeCollectionNew = new ArrayList<PropertyType>();
  189. for (PropertyType propertyTypeCollectionNewPropertyTypeToAttach : propertyTypeCollectionNew) {
  190. propertyTypeCollectionNewPropertyTypeToAttach = em.getReference(propertyTypeCollectionNewPropertyTypeToAttach.getClass(), propertyTypeCollectionNewPropertyTypeToAttach.getPropertyType());
  191. attachedPropertyTypeCollectionNew.add(propertyTypeCollectionNewPropertyTypeToAttach);
  192. }
  193. propertyTypeCollectionNew = attachedPropertyTypeCollectionNew;
  194. propertyClass.setPropertyTypeCollection(propertyTypeCollectionNew);
  195. propertyClass = em.merge(propertyClass);
  196. for (PropertyType propertyTypeCollectionOldPropertyType : propertyTypeCollectionOld) {
  197. if (!propertyTypeCollectionNew.contains(propertyTypeCollectionOldPropertyType)) {
  198. propertyTypeCollectionOldPropertyType.setPropertyClass(null);
  199. propertyTypeCollectionOldPropertyType = em.merge(propertyTypeCollectionOldPropertyType);
  200. }
  201. }
  202. for (PropertyType propertyTypeCollectionNewPropertyType : propertyTypeCollectionNew) {
  203. if (!propertyTypeCollectionOld.contains(propertyTypeCollectionNewPropertyType)) {
  204. PropertyClass oldPropertyClassOfPropertyTypeCollectionNewPropertyType = propertyTypeCollectionNewPropertyType.getPropertyClass();
  205. propertyTypeCollectionNewPropertyType.setPropertyClass(propertyClass);
  206. propertyTypeCollectionNewPropertyType = em.merge(propertyTypeCollectionNewPropertyType);
  207. if (oldPropertyClassOfPropertyTypeCollectionNewPropertyType != null && !oldPropertyClassOfPropertyTypeCollectionNewPropertyType.equals(propertyClass)) {
  208. oldPropertyClassOfPropertyTypeCollectionNewPropertyType.getPropertyTypeCollection().remove(propertyTypeCollectionNewPropertyType);
  209. oldPropertyClassOfPropertyTypeCollectionNewPropertyType = em.merge(oldPropertyClassOfPropertyTypeCollectionNewPropertyType);
  210. }
  211. }
  212. }
  213. utx.commit();
  214. addSuccessMessage("PropertyClass was successfully updated.");
  215. } catch (Exception ex) {
  216. try {
  217. String msg = ex.getLocalizedMessage();
  218. if (msg != null && msg.length() > 0) {
  219. addErrorMessage(msg);
  220. } else if (getPropertyClassFromRequest() == null) {
  221. addErrorMessage("The propertyClass with id " + currentPropertyClassString + " no longer exists.");
  222. utx.rollback();
  223. return listSetup();
  224. } else {
  225. addErrorMessage("A persistence error occurred.");
  226. }
  227. utx.rollback();
  228. } catch (Exception e) {
  229. ensureAddErrorMessage(e, "An error occurred attempting to roll back the transaction.");
  230. }
  231. return null;
  232. } finally {
  233. em.close();
  234. }
  235. return detailSetup();
  236. }
  237.  
  238. public String destroy() {
  239. propertyClass = getPropertyClassFromRequest();
  240. if (propertyClass == null) {
  241. String currentPropertyClassString = getRequestParameter("jsfcrud.currentPropertyClass");
  242. addErrorMessage("The propertyClass with id " + currentPropertyClassString + " no longer exists.");
  243. String relatedControllerOutcome = relatedControllerOutcome();
  244. if (relatedControllerOutcome != null) {
  245. return relatedControllerOutcome;
  246. }
  247. return listSetup();
  248. }
  249. EntityManager em = getEntityManager();
  250. try {
  251. utx.begin();
  252. propertyClass = em.getReference(propertyClass.getClass(), propertyClass.getPropertyClass());
  253. Collection<PropertyType> propertyTypeCollection = propertyClass.getPropertyTypeCollection();
  254. for (PropertyType propertyTypeCollectionPropertyType : propertyTypeCollection) {
  255. propertyTypeCollectionPropertyType.setPropertyClass(null);
  256. propertyTypeCollectionPropertyType = em.merge(propertyTypeCollectionPropertyType);
  257. }
  258. em.remove(propertyClass);
  259. utx.commit();
  260. addSuccessMessage("PropertyClass was successfully deleted.");
  261. } catch (Exception ex) {
  262. try {
  263. ensureAddErrorMessage(ex, "A persistence error occurred.");
  264. utx.rollback();
  265. } catch (Exception e) {
  266. ensureAddErrorMessage(e, "An error occurred attempting to roll back the transaction.");
  267. }
  268. return null;
  269. } finally {
  270. em.close();
  271. }
  272. String relatedControllerOutcome = relatedControllerOutcome();
  273. if (relatedControllerOutcome != null) {
  274. return relatedControllerOutcome;
  275. }
  276. return listSetup();
  277. }
  278.  
  279. private PropertyClass getPropertyClassFromRequest() {
  280. String theId = getRequestParameter("jsfcrud.currentPropertyClass");
  281. return (PropertyClass) new PropertyClassConverter().getAsObject(FacesContext.getCurrentInstance(), null, theId);
  282. }
  283.  
  284. private String getRequestParameter(String key) {
  285. return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
  286. }
  287.  
  288. public List<PropertyClass> getPropertyClasss() {
  289. if (propertyClasss == null) {
  290. propertyClasss = getPropertyClasss(false);
  291. }
  292. return propertyClasss;
  293. }
  294.  
  295. public List<PropertyClass> getPropertyClasss(boolean all) {
  296. EntityManager em = getEntityManager();
  297. try {
  298. Query q = em.createQuery("select object(o) from PropertyClass as o");
  299. if (!all) {
  300. q.setMaxResults(batchSize);
  301. q.setFirstResult(getFirstItem());
  302. }
  303. return q.getResultList();
  304. } finally {
  305. em.close();
  306. }
  307. }
  308.  
  309. private void ensureAddErrorMessage(Exception ex, String defaultMsg) {
  310. String msg = ex.getLocalizedMessage();
  311. if (msg != null && msg.length() > 0) {
  312. addErrorMessage(msg);
  313. } else {
  314. addErrorMessage(defaultMsg);
  315. }
  316. }
  317.  
  318. public static void addErrorMessage(String msg) {
  319. FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
  320. FacesContext.getCurrentInstance().addMessage(null, facesMsg);
  321. }
  322.  
  323. public static void addSuccessMessage(String msg) {
  324. FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
  325. FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
  326. }
  327.  
  328. public PropertyClass findPropertyClass(Integer id) {
  329. EntityManager em = getEntityManager();
  330. try {
  331. PropertyClass o = (PropertyClass) em.find(PropertyClass.class, id);
  332. return o;
  333. } finally {
  334. em.close();
  335. }
  336. }
  337.  
  338. public int getItemCount() {
  339. if (itemCount == -1) {
  340. EntityManager em = getEntityManager();
  341. try {
  342. itemCount = ((Long) em.createQuery("select count(o) from PropertyClass as o").getSingleResult()).intValue();
  343. } finally {
  344. em.close();
  345. }
  346. }
  347. return itemCount;
  348. }
  349.  
  350. public int getFirstItem() {
  351. getItemCount();
  352. if (firstItem >= itemCount) {
  353. if (itemCount == 0) {
  354. firstItem = 0;
  355. } else {
  356. int zeroBasedItemCount = itemCount - 1;
  357. double pageDouble = zeroBasedItemCount / batchSize;
  358. int page = (int) Math.floor(pageDouble);
  359. firstItem = page * batchSize;
  360. }
  361. }
  362. return firstItem;
  363. }
  364.  
  365. public int getLastItem() {
  366. getFirstItem();
  367. return firstItem + batchSize > itemCount ? itemCount : firstItem + batchSize;
  368. }
  369.  
  370. public int getBatchSize() {
  371. return batchSize;
  372. }
  373.  
  374. public String next() {
  375. reset(false);
  376. getFirstItem();
  377. if (firstItem + batchSize < itemCount) {
  378. firstItem += batchSize;
  379. }
  380. return "propertyClass_list";
  381. }
  382.  
  383. public String prev() {
  384. reset(false);
  385. getFirstItem();
  386. firstItem -= batchSize;
  387. if (firstItem < 0) {
  388. firstItem = 0;
  389. }
  390. return "propertyClass_list";
  391. }
  392.  
  393. private String relatedControllerOutcome() {
  394. String relatedControllerString = getRequestParameter("jsfcrud.relatedController");
  395. String relatedControllerTypeString = getRequestParameter("jsfcrud.relatedControllerType");
  396. if (relatedControllerString != null && relatedControllerTypeString != null) {
  397. FacesContext context = FacesContext.getCurrentInstance();
  398. Object relatedController = context.getApplication().getELResolver().getValue(context.getELContext(), null, relatedControllerString);
  399. try {
  400. Class<?> relatedControllerType = Class.forName(relatedControllerTypeString);
  401. Method detailSetupMethod = relatedControllerType.getMethod("detailSetup");
  402. return (String) detailSetupMethod.invoke(relatedController);
  403. } catch (ClassNotFoundException e) {
  404. throw new FacesException(e);
  405. } catch (NoSuchMethodException e) {
  406. throw new FacesException(e);
  407. } catch (IllegalAccessException e) {
  408. throw new FacesException(e);
  409. } catch (InvocationTargetException e) {
  410. throw new FacesException(e);
  411. }
  412. }
  413. return null;
  414. }
  415.  
  416. private void reset(boolean resetFirstItem) {
  417. propertyClass = null;
  418. propertyClasss = null;
  419. itemCount = -1;
  420. if (resetFirstItem) {
  421. firstItem = 0;
  422. }
  423. }
  424. private Map<Object, String> asString = null;
  425.  
  426. public Map<Object, String> getAsString() {
  427. if (asString == null) {
  428. asString = new HashMap<Object, String>() {
  429.  
  430. @Override
  431. public String get(Object key) {
  432. if (key instanceof Object[]) {
  433. Object[] keyAsArray = (Object[]) key;
  434. if (keyAsArray.length == 0) {
  435. return "(No Items)";
  436. }
  437. StringBuffer sb = new StringBuffer();
  438. for (int i = 0; i < keyAsArray.length; i++) {
  439. if (i > 0) {
  440. sb.append("<br />");
  441. }
  442. sb.append(keyAsArray[i]);
  443. }
  444. return sb.toString();
  445. }
  446. return new PropertyClassConverter().getAsString(FacesContext.getCurrentInstance(), null, (PropertyClass) key);
  447. }
  448. };
  449. }
  450. return asString;
  451. }
  452.  
  453. public void validateCreate(FacesContext facesContext, UIComponent component, Object value) {
  454. PropertyClassConverter converter = new PropertyClassConverter();
  455. String newPropertyClassString = converter.getAsString(FacesContext.getCurrentInstance(), null, new PropertyClass());
  456. String propertyClassString = converter.getAsString(FacesContext.getCurrentInstance(), null, propertyClass);
  457. if (!newPropertyClassString.equals(propertyClassString)) {
  458. createSetup();
  459. }
  460. }
  461.  
  462. }

PropertyClassConverter.java
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package entities;
  7.  
  8. import javax.faces.component.UIComponent;
  9. import javax.faces.context.FacesContext;
  10. import javax.faces.convert.Converter;
  11.  
  12. /**
  13.  *
  14.  * @author kohuke
  15.  */
  16. public class PropertyClassConverter implements Converter {
  17.  
  18. public Object getAsObject(FacesContext facesContext, UIComponent component, String string) {
  19. if (string == null || string.length() == 0) {
  20. return null;
  21. }
  22. Integer id = new Integer(string);
  23. PropertyClassController controller = (PropertyClassController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "propertyClass");
  24.  
  25. return controller.findPropertyClass(id);
  26. }
  27.  
  28. public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
  29. if (object == null) {
  30. return null;
  31. }
  32. if (object instanceof PropertyClass) {
  33. PropertyClass o = (PropertyClass) object;
  34. return o.getPropertyClass() == null ? "" : o.getPropertyClass().toString();
  35. } else {
  36. throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: entities.PropertyClass");
  37. }
  38. }
  39.  
  40. }

PropertyController.java
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package entities;
  7.  
  8. import java.util.List;
  9. import java.util.Map;
  10. import javax.annotation.Resource;
  11. import javax.faces.component.UIComponent;
  12. import javax.faces.context.FacesContext;
  13. import javax.faces.model.SelectItem;
  14. import javax.persistence.EntityManager;
  15. import javax.persistence.EntityManagerFactory;
  16. import javax.persistence.PersistenceUnit;
  17. import javax.persistence.Query;
  18. import javax.faces.application.FacesMessage;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.lang.reflect.Method;
  21. import javax.faces.FacesException;
  22. import java.util.HashMap;
  23. import javax.faces.validator.ValidatorException;
  24. import javax.transaction.UserTransaction;
  25.  
  26. /**
  27.  *
  28.  * @author kohuke
  29.  */
  30. public class PropertyController {
  31. private Property property = null;
  32. private List<Property> propertys = null;
  33. @Resource
  34. private UserTransaction utx = null;
  35. @PersistenceUnit(unitName = "Varad2PU")
  36. private EntityManagerFactory emf = null;
  37.  
  38. public EntityManager getEntityManager() {
  39. return emf.createEntityManager();
  40. }
  41. public int batchSize = 5;
  42. private int firstItem = 0;
  43. private int itemCount = -1;
  44.  
  45. public SelectItem[] getPropertysAvailableSelectMany() {
  46. return getPropertysAvailable(false);
  47. }
  48.  
  49. public SelectItem[] getPropertysAvailableSelectOne() {
  50. return getPropertysAvailable(true);
  51. }
  52.  
  53. private SelectItem[] getPropertysAvailable(boolean one) {
  54. List<Property> allPropertys = getPropertys(true);
  55. int size = one ? allPropertys.size() + 1 : allPropertys.size();
  56. SelectItem[] items = new SelectItem[size];
  57. int i = 0;
  58. if (one) {
  59. items[0] = new SelectItem("", "---");
  60. i++;
  61. }
  62. for (Property x : allPropertys) {
  63. items[i++] = new SelectItem(x, x.toString());
  64. }
  65. return items;
  66. }
  67.  
  68. public Property getProperty() {
  69. if (property == null) {
  70. property = getPropertyFromRequest();
  71. }
  72. if (property == null) {
  73. property = new Property();
  74. }
  75. return property;
  76. }
  77.  
  78. public String listSetup() {
  79. reset(true);
  80. return "property_list";
  81. }
  82.  
  83. public String createSetup() {
  84. reset(false);
  85. property = new Property();
  86. return "property_create";
  87. }
  88.  
  89. public String create() {
  90. EntityManager em = getEntityManager();
  91. try {
  92. utx.begin();
  93. Employee createdBy = property.getCreatedBy();
  94. if (createdBy != null) {
  95. createdBy = em.getReference(createdBy.getClass(), createdBy.getEmployee());
  96. property.setCreatedBy(createdBy);
  97. }
  98. PropertyType propertyType = property.getPropertyType();
  99. if (propertyType != null) {
  100. propertyType = em.getReference(propertyType.getClass(), propertyType.getPropertyType());
  101. property.setPropertyType(propertyType);
  102. }
  103. Employee updatedBy = property.getUpdatedBy();
  104. if (updatedBy != null) {
  105. updatedBy = em.getReference(updatedBy.getClass(), updatedBy.getEmployee());
  106. property.setUpdatedBy(updatedBy);
  107. }
  108. em.persist(property);
  109. if (createdBy != null) {
  110. createdBy.getPropertyCollection().add(property);
  111. createdBy = em.merge(createdBy);
  112. }
  113. if (propertyType != null) {
  114. propertyType.getPropertyCollection().add(property);
  115. propertyType = em.merge(propertyType);
  116. }
  117. if (updatedBy != null) {
  118. updatedBy.getPropertyCollection().add(property);
  119. updatedBy = em.merge(updatedBy);
  120. }
  121. utx.commit();
  122. addSuccessMessage("Property was successfully created.");
  123. } catch (Exception ex) {
  124. try {
  125. ensureAddErrorMessage(ex, "A persistence error occurred.");
  126. utx.rollback();
  127. } catch (Exception e) {
  128. ensureAddErrorMessage(e, "An error occurred attempting to roll back the transaction.");
  129. }
  130. return null;
  131. } finally {
  132. em.close();
  133. }
  134. return listSetup();
  135. }
  136.  
  137. public String detailSetup() {
  138. return scalarSetup("property_detail");
  139. }
  140.  
  141. public String editSetup() {
  142. return scalarSetup("property_edit");
  143. }
  144.  
  145. private String scalarSetup(String destination) {
  146. reset(false);
  147. property = getPropertyFromRequest();
  148. if (property == null) {
  149. String requestPropertyString = getRequestParameter("jsfcrud.currentProperty");
  150. addErrorMessage("The property with id " + requestPropertyString + " no longer exists.");
  151. String relatedControllerOutcome = relatedControllerOutcome();
  152. if (relatedControllerOutcome != null) {
  153. return relatedControllerOutcome;
  154. }
  155. return listSetup();
  156. }
  157. return destination;
  158. }
  159.  
  160. public String edit() {
  161. PropertyConverter converter = new PropertyConverter();
  162. String propertyString = converter.getAsString(FacesContext.getCurrentInstance(), null, property);
  163. String currentPropertyString = getRequestParameter("jsfcrud.currentProperty");
  164. if (propertyString == null || propertyString.length() == 0 || !propertyString.equals(currentPropertyString)) {
  165. String outcome = editSetup();
  166. if ("property_edit".equals(outcome)) {
  167. addErrorMessage("Could not edit property. Try again.");
  168. }
  169. return outcome;
  170. }
  171. EntityManager em = getEntityManager();
  172. try {
  173. utx.begin();
  174. Property persistentProperty = em.find(Property.class, property.getProperty());
  175. Employee createdByOld = persistentProperty.getCreatedBy();
  176. Employee createdByNew = property.getCreatedBy();
  177. PropertyType propertyTypeOld = persistentProperty.getPropertyType();
  178. PropertyType propertyTypeNew = property.getPropertyType();
  179. Employee updatedByOld = persistentProperty.getUpdatedBy();
  180. Employee updatedByNew = property.getUpdatedBy();
  181. if (createdByNew != null) {
  182. createdByNew = em.getReference(createdByNew.getClass(), createdByNew.getEmployee());
  183. property.setCreatedBy(createdByNew);
  184. }
  185. if (propertyTypeNew != null) {
  186. propertyTypeNew = em.getReference(propertyTypeNew.getClass(), propertyTypeNew.getPropertyType());
  187. property.setPropertyType(propertyTypeNew);
  188. }
  189. if (updatedByNew != null) {
  190. updatedByNew = em.getReference(updatedByNew.getClass(), updatedByNew.getEmployee());
  191. property.setUpdatedBy(updatedByNew);
  192. }
  193. property = em.merge(property);
  194. if (createdByOld != null && !createdByOld.equals(createdByNew)) {
  195. createdByOld.getPropertyCollection().remove(property);
  196. createdByOld = em.merge(createdByOld);
  197. }
  198. if (createdByNew != null && !createdByNew.equals(createdByOld)) {
  199. createdByNew.getPropertyCollection().add(property);
  200. createdByNew = em.merge(createdByNew);
  201. }
  202. if (propertyTypeOld != null && !propertyTypeOld.equals(propertyTypeNew)) {
  203. propertyTypeOld.getPropertyCollection().remove(property);
  204. propertyTypeOld = em.merge(propertyTypeOld);
  205. }
  206. if (propertyTypeNew != null && !propertyTypeNew.equals(propertyTypeOld)) {
  207. propertyTypeNew.getPropertyCollection().add(property);
  208. propertyTypeNew = em.merge(propertyTypeNew);
  209. }
  210. if (updatedByOld != null && !updatedByOld.equals(updatedByNew)) {
  211. updatedByOld.getPropertyCollection().remove(property);
  212. updatedByOld = em.merge(updatedByOld);
  213. }
  214. if (updatedByNew != null && !updatedByNew.equals(updatedByOld)) {
  215. updatedByNew.getPropertyCollection().add(property);
  216. updatedByNew = em.merge(updatedByNew);
  217. }
  218. utx.commit();
  219. addSuccessMessage("Property was successfully updated.");
  220. } catch (Exception ex) {
  221. try {
  222. String msg = ex.getLocalizedMessage();
  223. if (msg != null && msg.length() > 0) {
  224. addErrorMessage(msg);
  225. } else if (getPropertyFromRequest() == null) {
  226. addErrorMessage("The property with id " + currentPropertyString + " no longer exists.");
  227. utx.rollback();
  228. return listSetup();
  229. } else {
  230. addErrorMessage("A persistence error occurred.");
  231. }
  232. utx.rollback();
  233. } catch (Exception e) {
  234. ensureAddErrorMessage(e, "An error occurred attempting to roll back the transaction.");
  235. }
  236. return null;
  237. } finally {
  238. em.close();
  239. }
  240. return detailSetup();
  241. }
  242.  
  243. public String destroy() {
  244. property = getPropertyFromRequest();
  245. if (property == null) {
  246. String currentPropertyString = getRequestParameter("jsfcrud.currentProperty");
  247. addErrorMessage("The property with id " + currentPropertyString + " no longer exists.");
  248. String relatedControllerOutcome = relatedControllerOutcome();
  249. if (relatedControllerOutcome != null) {
  250. return relatedControllerOutcome;
  251. }
  252. return listSetup();
  253. }
  254. EntityManager em = getEntityManager();
  255. try {
  256. utx.begin();
  257. property = em.getReference(property.getClass(), property.getProperty());
  258. Employee createdBy = property.getCreatedBy();
  259. if (createdBy != null) {
  260. createdBy.getPropertyCollection().remove(property);
  261. createdBy = em.merge(createdBy);
  262. }
  263. PropertyType propertyType = property.getPropertyType();
  264. if (propertyType != null) {
  265. propertyType.getPropertyCollection().remove(property);
  266. propertyType = em.merge(propertyType);
  267. }
  268. Employee updatedBy = property.getUpdatedBy();
  269. if (updatedBy != null) {
  270. updatedBy.getPropertyCollection().remove(property);
  271. updatedBy = em.merge(updatedBy);
  272. }
  273. em.remove(property);
  274. utx.commit();
  275. addSuccessMessage("Property was successfully deleted.");
  276. } catch (Exception ex) {
  277. try {
  278. ensureAddErrorMessage(ex, "A persistence error occurred.");
  279. utx.rollback();
  280. } catch (Exception e) {
  281. ensureAddErrorMessage(e, "An error occurred attempting to roll back the transaction.");
  282. }
  283. return null;
  284. } finally {
  285. em.close();
  286. }
  287. String relatedControllerOutcome = relatedControllerOutcome();
  288. if (relatedControllerOutcome != null) {
  289. return relatedControllerOutcome;
  290. }
  291. return listSetup();
  292. }
  293.  
  294. private Property getPropertyFromRequest() {
  295. String theId = getRequestParameter("jsfcrud.currentProperty");
  296. return (Property) new PropertyConverter().getAsObject(FacesContext.getCurrentInstance(), null, theId);
  297. }
  298.  
  299. private String getRequestParameter(String key) {
  300. return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
  301. }
  302.  
  303. public List<Property> getPropertys() {
  304. if (propertys == null) {
  305. propertys = getPropertys(false);
  306. }
  307. return propertys;
  308. }
  309.  
  310. public List<Property> getPropertys(boolean all) {
  311. EntityManager em = getEntityManager();
  312. try {
  313. Query q = em.createQuery("select object(o) from Property as o");
  314. if (!all) {
  315. q.setMaxResults(batchSize);
  316. q.setFirstResult(getFirstItem());
  317. }
  318. return q.getResultList();
  319. } finally {
  320. em.close();
  321. }
  322. }
  323.  
  324. private void ensureAddErrorMessage(Exception ex, String defaultMsg) {
  325. String msg = ex.getLocalizedMessage();
  326. if (msg != null && msg.length() > 0) {
  327. addErrorMessage(msg);
  328. } else {
  329. addErrorMessage(defaultMsg);
  330. }
  331. }
  332.  
  333. public static void addErrorMessage(String msg) {
  334. FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
  335. FacesContext.getCurrentInstance().addMessage(null, facesMsg);
  336. }
  337.  
  338. public static void addSuccessMessage(String msg) {
  339. FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
  340. FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
  341. }
  342.  
  343. public Property findProperty(Integer id) {
  344. EntityManager em = getEntityManager();
  345. try {
  346. Property o = (Property) em.find(Property.class, id);
  347. return o;
  348. } finally {
  349. em.close();
  350. }
  351. }
  352.  
  353. public int getItemCount() {
  354. if (itemCount == -1) {
  355. EntityManager em = getEntityManager();
  356. try {
  357. itemCount = ((Long) em.createQuery("select count(o) from Property as o").getSingleResult()).intValue();
  358. } finally {
  359. em.close();
  360. }
  361. }
  362. return itemCount;
  363. }
  364.  
  365. public int getFirstItem() {
  366. getItemCount();
  367. if (firstItem >= itemCount) {
  368. if (itemCount == 0) {
  369. firstItem = 0;
  370. } else {
  371. int zeroBasedItemCount = itemCount - 1;
  372. double pageDouble = zeroBasedItemCount / batchSize;
  373. int page = (int) Math.floor(pageDouble);
  374. firstItem = page * batchSize;
  375. }
  376. }
  377. return firstItem;
  378. }
  379.  
  380. public int getLastItem() {
  381. getFirstItem();
  382. return firstItem + batchSize > itemCount ? itemCount : firstItem + batchSize;
  383. }
  384.  
  385. public int getBatchSize() {
  386. return batchSize;
  387. }
  388.  
  389. public String next() {
  390. reset(false);
  391. getFirstItem();
  392. if (firstItem + batchSize < itemCount) {
  393. firstItem += batchSize;
  394. }
  395. return "property_list";
  396. }
  397.  
  398. public String prev() {
  399. reset(false);
  400. getFirstItem();
  401. firstItem -= batchSize;
  402. if (firstItem < 0) {
  403. firstItem = 0;
  404. }
  405. return "property_list";
  406. }
  407.  
  408. private String relatedControllerOutcome() {
  409. String relatedControllerString = getRequestParameter("jsfcrud.relatedController");
  410. String relatedControllerTypeString = getRequestParameter("jsfcrud.relatedControllerType");
  411. if (relatedControllerString != null && relatedControllerTypeString != null) {
  412. FacesContext context = FacesContext.getCurrentInstance();
  413. Object relatedController = context.getApplication().getELResolver().getValue(context.getELContext(), null, relatedControllerString);
  414. try {
  415. Class<?> relatedControllerType = Class.forName(relatedControllerTypeString);
  416. Method detailSetupMethod = relatedControllerType.getMethod("detailSetup");
  417. return (String) detailSetupMethod.invoke(relatedController);
  418. } catch (ClassNotFoundException e) {
  419. throw new FacesException(e);
  420. } catch (NoSuchMethodException e) {
  421. throw new FacesException(e);
  422. } catch (IllegalAccessException e) {
  423. throw new FacesException(e);
  424. } catch (InvocationTargetException e) {
  425. throw new FacesException(e);
  426. }
  427. }
  428. return null;
  429. }
  430.  
  431. private void reset(boolean resetFirstItem) {
  432. property = null;
  433. propertys = null;
  434. itemCount = -1;
  435. if (resetFirstItem) {
  436. firstItem = 0;
  437. }
  438. }
  439. private Map<Object, String> asString = null;
  440.  
  441. public Map<Object, String> getAsString() {
  442. if (asString == null) {
  443. asString = new HashMap<Object, String>() {
  444.  
  445. @Override
  446. public String get(Object key) {
  447. if (key instanceof Object[]) {
  448. Object[] keyAsArray = (Object[]) key;
  449. if (keyAsArray.length == 0) {
  450. return "(No Items)";
  451. }
  452. StringBuffer sb = new StringBuffer();
  453. for (int i = 0; i < keyAsArray.length; i++) {
  454. if (i > 0) {
  455. sb.append("<br />");
  456. }
  457. sb.append(keyAsArray[i]);
  458. }
  459. return sb.toString();
  460. }
  461. return new PropertyConverter().getAsString(FacesContext.getCurrentInstance(), null, (Property) key);
  462. }
  463. };
  464. }
  465. return asString;
  466. }
  467.  
  468. public void validateCreate(FacesContext facesContext, UIComponent component, Object value) {
  469. PropertyConverter converter = new PropertyConverter();
  470. String newPropertyString = converter.getAsString(FacesContext.getCurrentInstance(), null, new Property());
  471. String propertyString = converter.getAsString(FacesContext.getCurrentInstance(), null, property);
  472. if (!newPropertyString.equals(propertyString)) {
  473. createSetup();
  474. }
  475. }
  476.  
  477. }

PropertyConverter.java:
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package entities;
  7.  
  8. import javax.faces.component.UIComponent;
  9. import javax.faces.context.FacesContext;
  10. import javax.faces.convert.Converter;
  11.  
  12. /**
  13.  *
  14.  * @author kohuke
  15.  */
  16. public class PropertyConverter implements Converter {
  17.  
  18. public Object getAsObject(FacesContext facesContext, UIComponent component, String string) {
  19. if (string == null || string.length() == 0) {
  20. return null;
  21. }
  22. Integer id = new Integer(string);
  23. PropertyController controller = (PropertyController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "property");
  24.  
  25. return controller.findProperty(id);
  26. }
  27.  
  28. public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
  29. if (object == null) {
  30. return null;
  31. }
  32. if (object instanceof Property) {
  33. Property o = (Property) object;
  34. return o.getProperty() == null ? "" : o.getProperty().toString();
  35. } else {
  36. throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: entities.Property");
  37. }
  38. }
  39.  
  40. }

context.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Context path="/Varad2">
  3. <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/varad" password="******" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/varad" username="root"/>
  4. </Context>

web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  3. <context-param>
  4. <param-name>com.sun.faces.verifyObjects</param-name>
  5. <param-value>true</param-value>
  6. </context-param>
  7. <context-param>
  8. <param-name>com.sun.faces.validateXml</param-name>
  9. <param-value>true</param-value>
  10. </context-param>
  11. <context-param>
  12. <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  13. <param-value>client</param-value>
  14. </context-param>
  15. <servlet>
  16. <servlet-name>Faces Servlet</servlet-name>
  17. <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  18. <load-on-startup>1</load-on-startup>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>Faces Servlet</servlet-name>
  22. <url-pattern>/faces/*</url-pattern>
  23.   </servlet-mapping>
  24.   <session-config>
  25.   <session-timeout>
  26.   30
  27.   </session-timeout>
  28.   </session-config>
  29.   <welcome-file-list>
  30.   <welcome-file>faces/welcomeJSF.jsp</welcome-file>
  31.   </welcome-file-list>
  32.   </web-app>
  33.  

FacesConfig.xml
  1. <?xml version='1.0' encoding='UTF-8'?>
  2.  
  3. <!-- =========== FULL CONFIGURATION FILE ================================== -->
  4.  
  5. <faces-config version="1.2"
  6. xmlns="http://java.sun.com/xml/ns/javaee"
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
  9. <managed-bean>
  10. <managed-bean-name>employee</managed-bean-name>
  11. <managed-bean-class>entities.EmployeeController</managed-bean-class>
  12. <managed-bean-scope>session</managed-bean-scope>
  13. </managed-bean>
  14. <converter>
  15. <converter-for-class>entities.Employee</converter-for-class>
  16. <converter-class>entities.EmployeeConverter</converter-class>
  17. </converter>
  18. <navigation-rule>
  19. <navigation-case>
  20. <from-outcome>employee_create</from-outcome>
  21. <to-view-id>/beans/employee/New.jsp</to-view-id>
  22. </navigation-case>
  23. </navigation-rule>
  24. <navigation-rule>
  25. <navigation-case>
  26. <from-outcome>employee_list</from-outcome>
  27. <to-view-id>/beans/employee/List.jsp</to-view-id>
  28. </navigation-case>
  29. </navigation-rule>
  30. <navigation-rule>
  31. <navigation-case>
  32. <from-outcome>employee_edit</from-outcome>
  33. <to-view-id>/beans/employee/Edit.jsp</to-view-id>
  34. </navigation-case>
  35. </navigation-rule>
  36. <navigation-rule>
  37. <navigation-case>
  38. <from-outcome>employee_detail</from-outcome>
  39. <to-view-id>/beans/employee/Detail.jsp</to-view-id>
  40. </navigation-case>
  41. </navigation-rule>
  42. <managed-bean>
  43. <managed-bean-name>property</managed-bean-name>
  44. <managed-bean-class>entities.PropertyController</managed-bean-class>
  45. <managed-bean-scope>session</managed-bean-scope>
  46. </managed-bean>
  47. <converter>
  48. <converter-for-class>entities.Property</converter-for-class>
  49. <converter-class>entities.PropertyConverter</converter-class>
  50. </converter>
  51. <navigation-rule>
  52. <navigation-case>
  53. <from-outcome>property_create</from-outcome>
  54. <to-view-id>/beans/property/New.jsp</to-view-id>
  55. </navigation-case>
  56. </navigation-rule>
  57. <navigation-rule>
  58. <navigation-case>
  59. <from-outcome>property_list</from-outcome>
  60. <to-view-id>/beans/property/List.jsp</to-view-id>
  61. </navigation-case>
  62. </navigation-rule>
  63. <navigation-rule>
  64. <navigation-case>
  65. <from-outcome>property_edit</from-outcome>
  66. <to-view-id>/beans/property/Edit.jsp</to-view-id>
  67. </navigation-case>
  68. </navigation-rule>
  69. <navigation-rule>
  70. <navigation-case>
  71. <from-outcome>property_detail</from-outcome>
  72. <to-view-id>/beans/property/Detail.jsp</to-view-id>
  73. </navigation-case>
  74. </navigation-rule>
  75. <managed-bean>
  76. <managed-bean-name>propertyClass</managed-bean-name>
  77. <managed-bean-class>entities.PropertyClassController</managed-bean-class>
  78. <managed-bean-scope>session</managed-bean-scope>
  79. </managed-bean>
  80. <converter>
  81. <converter-for-class>entities.PropertyClass</converter-for-class>
  82. <converter-class>entities.PropertyClassConverter</converter-class>
  83. </converter>
  84. <navigation-rule>
  85. <navigation-case>
  86. <from-outcome>propertyClass_create</from-outcome>
  87. <to-view-id>/beans/propertyClass/New.jsp</to-view-id>
  88. </navigation-case>
  89. </navigation-rule>
  90. <navigation-rule>
  91. <navigation-case>
  92. <from-outcome>propertyClass_list</from-outcome>
  93. <to-view-id>/beans/propertyClass/List.jsp</to-view-id>
  94. </navigation-case>
  95. </navigation-rule>
  96. <navigation-rule>
  97. <navigation-case>
  98. <from-outcome>propertyClass_edit</from-outcome>
  99. <to-view-id>/beans/propertyClass/Edit.jsp</to-view-id>
  100. </navigation-case>
  101. </navigation-rule>
  102. <navigation-rule>
  103. <navigation-case>
  104. <from-outcome>propertyClass_detail</from-outcome>
  105. <to-view-id>/beans/propertyClass/Detail.jsp</to-view-id>
  106. </navigation-case>
  107. </navigation-rule>
  108. <managed-bean>
  109. <managed-bean-name>propertyType</managed-bean-name>
  110. <managed-bean-class>entities.PropertyTypeController</managed-bean-class>
  111. <managed-bean-scope>session</managed-bean-scope>
  112. </managed-bean>
  113. <converter>
  114. <converter-for-class>entities.PropertyType</converter-for-class>
  115. <converter-class>entities.PropertyTypeConverter</converter-class>
  116. </converter>
  117. <navigation-rule>
  118. <navigation-case>
  119. <from-outcome>propertyType_create</from-outcome>
  120. <to-view-id>/beans/propertyType/New.jsp</to-view-id>
  121. </navigation-case>
  122. </navigation-rule>
  123. <navigation-rule>
  124. <navigation-case>
  125. <from-outcome>propertyType_list</from-outcome>
  126. <to-view-id>/beans/propertyType/List.jsp</to-view-id>
  127. </navigation-case>
  128. </navigation-rule>
  129. <navigation-rule>
  130. <navigation-case>
  131. <from-outcome>propertyType_edit</from-outcome>
  132. <to-view-id>/beans/propertyType/Edit.jsp</to-view-id>
  133. </navigation-case>
  134. </navigation-rule>
  135. <navigation-rule>
  136. <navigation-case>
  137. <from-outcome>propertyType_detail</from-outcome>
  138. <to-view-id>/beans/propertyType/Detail.jsp</to-view-id>
  139. </navigation-case>
  140. </navigation-rule>
  141. </faces-config>

I hope this is enough code for you to help me. Also i'll attach my entire project if anyone needs to see the entire code.
All i need to know what i have to edit in order to make this thing work.
Thanks if you find the time the answer. The sooner the better
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC