[Logo] Mentawai Forum - Mentawai Web Framework
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 

Forum Read-Only! Check the new site and forum here!

Messages posted by: navigo  XML
Profile for navigo -> Messages posted by navigo [7]
Author Message
org.mentawai.db.ODBConnectionHandler.java:
Code:
package org.mentawai.db;
 
 import java.util.ArrayList;
 import org.neodatis.odb.ODB;
 import org.neodatis.odb.ODBFactory;
 
 /**
  *
  * @author navigo
  */
 public class ODBConnectionHandler
 {
 
 	private String host;
 	private int port;
 	private String dbName;
 	private int poolSize;
 	ArrayList<ODB> availableODB;
 	ArrayList<ODB> usedODB;
 
 	public ODBConnectionHandler(String host, int port, String dbName, int poolSize)
 	{
 		this.host = host;
 		this.port = port;
 		this.dbName = dbName;
 		this.poolSize = poolSize;
 		this.availableODB = new ArrayList<ODB>();
 		this.usedODB = new ArrayList<ODB>();
 
 		ODB odb;
 		for(int i = 0; i < this.poolSize; i++)
 		{
 			odb = ODBFactory.openClient(host, port, dbName);
 			availableODB.add(odb);
 		}
 	}
 
 	public ODB getConnection()
 	{
 		ODB odb;
 		if (availableODB.size() > 0)
 		{
 			odb = availableODB.remove(0);
 			if (odb.isClosed())
 			{
 				odb = ODBFactory.openClient(host, port, dbName);
 			}
 			usedODB.add(odb);
 		}
 		else
 		{
 			System.out.println("ODB Client pool exhausted; adding another client.");
 			odb = ODBFactory.openClient(host, port, dbName);
 			usedODB.add(odb);
 		}
 		return odb;
 	}
 
 	public void release(ODB odb)
 	{
 		if(odb != null)
 		{
 			if(! odb.isClosed())
 			{
 				usedODB.remove(odb);
 				availableODB.add(odb);
 			}
 			else
 			{
 				System.out.println("Returned ODB Client is closed; dropping.");
 			}
 		}
 	}
 
 	public void destroy()
 	{
 		for(ODB myODB : availableODB)
 		{
 			myODB.close();
 		}
 		for(ODB myODB : usedODB)
 		{
 			myODB.close();
 		}
 	}
 }
 


org.mentawai.filter.NeoDatisFilter.java:
Code:
package org.mentawai.filter;
 
 import org.mentawai.core.Action;
 import org.mentawai.core.AfterConsequenceFilter;
 import org.mentawai.core.Consequence;
 import org.mentawai.core.InputWrapper;
 import org.mentawai.core.InvocationChain;
 import org.mentawai.db.ODBConnectionHandler;
 import org.neodatis.odb.ODB;
 
 /**
  *
  * @author navigo
  */
 public class NeoDatisFilter extends InputWrapper implements AfterConsequenceFilter
 {
 	public static final String KEY = "conn";
 	private String connKey;
 	private final ODBConnectionHandler odbConnectionHandler;
 	private ThreadLocal<ODB> odb = new ThreadLocal<ODB>();
 
 	public NeoDatisFilter(String connKey, ODBConnectionHandler odbConnectionHandler)
 	{
 		super();
 		this.connKey = connKey;
 		this.odbConnectionHandler = odbConnectionHandler;
 	}
 
 	public NeoDatisFilter(ODBConnectionHandler odbConnectionHandler)
 	{
 		this(KEY, odbConnectionHandler);
 	}
 
 	public NeoDatisFilter(ODBConnectionHandler odbConnectionHandler, String connKey)
 	{
 		this(connKey, odbConnectionHandler);
 	}
 
 	public String filter(InvocationChain chain) throws Exception
 	{
 		Action action = chain.getAction();
 		super.setInput(action.getInput());
 		action.setInput(this);
 		return chain.invoke();
 	}
 
 	public void destroy()
 	{
 		odbConnectionHandler.destroy();
 	}
 
 	public void afterConsequence(Action action, Consequence cnsqnc, boolean bln, boolean bln1, String string)
 	{
 		ODB myODB = this.odb.get();
 		if(myODB != null)
 		{
 			this.odb.set(null);
 			removeValue(connKey);
 			odbConnectionHandler.release(myODB);
 		}
 	}
 
 	@Override
 	public Object getValue(String key)
 	{
 		if(key.equals(connKey))
 		{
 			ODB myODB = odb.get();
 			if(myODB == null)
 			{
 				try
 				{
 					myODB = odbConnectionHandler.getConnection();
 					odb.set(myODB);
 					setValue(connKey, myODB);
 				}
 				catch(Exception e)
 				{
 					throw new RuntimeException("Error while getting the connection from the connection handler '" + odbConnectionHandler + "': " + e, e);
 				}
 			}
 			return myODB;
 		}
 		return super.getValue(key);
 	}
 }
 


These allow you to use AutoWiring in your application with a great object open source database http://neodatis.org

Use these in your Application Manager as such:
Code:
public class ApplicationManager extends org.mentawai.core.ApplicationManager
 {
 
 	private ODBConnectionHandler odbch = null;
 
 	@Override
 	public void init(Context application)
 	{
 		setDebugMode(true);
 		odbch = new ODBConnectionHandler("localhost", 9090, "main", 1);
 	}
 .............snip
 		//Connection Pooling
 		filter(new NeoDatisFilter("odb", odbch));
 .............snip
 		//IoC components (DAOs)
 		ioc("userAccountDAO", NeoDatisUserAccountDAO.class);
 
 		//Auto-wiring
 		filter(new AutoWiringFilter());
 
 		//Dependencies
 		aw("odb", ODB.class); //DAOs depend on ODB
 .............snip


That should get the general idea across. Feel free to comment or correct.
no, I don't speak spanish / portuguese (?),

but, I wound up referring to this post, and it helped me get the job done.

http://forum.mentaframework.org/posts/list/1132.page

Thanks!
Hello All,

I am looking for any guidance on how to make mentawai framework push a CSV file out to a browser (which is then saved as a text file) instead of rendering an HTML file.

For instance, I have a report that can be run, and you can step through the pages in html, but I also want an option where they can download the results in CSV.

What I have thought of thus far, and no, I don't like it - is to create a file in a temporary directory on the server, and then let the client just click a hyperlink to get the file. I would much rather stream the file to the browser....

Any comments / input?

Thanks.
RubemAzenha,

Thanks for the feedback. I think I have done that. What I have is using the validation to make sure the input is sane, then I use a "check" method to further check the input - i.e. I use checkEdit or checkAdd which checks the results from an Add or Edit.

Thanks again.
Hello All,

I am trying to write my own rule to use in validation to check values against a database. Is there a graceful way of accessing the C3P0 Pool I created in the ApplicationManager?

I know in my actions, I can refer to input.getValue("pd_conn"), but what is the best way in a rule to access the database? I know I could build a separate connection pool, but I anticipate a number of rules, and I would rather not have that many pools....

Any input is welcome.
Awesome,

All I had to do was post, then I found the answer.....

Code:
 
 <mtw:loop var="service">
 ....
 <mtw:loop>
 //here I can access the var from the service loop as service.id
 </mtw:loop>
 </mtw:loop>
 


Many thanks.

PS Also, mentaframework rocks. I spent alot of time looking at tapestry and this one won out because of the design philosophy, up to date documentation, and the seemingly "common sense" approach to getting things done. For the first time ever in java web development, I have been able to apply the "it seems like it ought to work this way" approach and have it work. Well done!
Hello:

Probably a very basic question, but being a newb, must ask it. I have an <mtw:loop> inside of an <mtw:loop>, and would like to access a value that was available in the parent loop. Is there a way to do that?

Code:
 		<mtw:list value="user.customer.services">
 		    <mtw:loop>
 			<tr>
 			    <th colspan="4"><mtw:out value="name"/></th>
 			</tr>
 			<tr>
 			    <th>Source Number</th>
 			    <th>Dialed Number</th>
 			    <th>Enabled ?</th>
 			    <th>Actions</th>
 			</tr>
 			<tr>
 			    <td> - </td>
 			    <td> - </td>
 			    <td> - </td>
 			    <td><a href="<mtw:contextPath />/CallBlockAction.add.mtw?id=<mtw:out value="id" />">Add</a></td>
 			</tr>
 			<mtw:list value="callBlocks">
 			    <mtw:loop>
 				<tr>
 				    <td><mtw:out value="sourceNumber" /></td>
 				    <td><mtw:out value="dialedNumber" /></td>
 				    <td><mtw:out value="enabled" /></td>
 				    <td>
 		*******               <a href="<mtw:contextPath />/CallBlockAction.edit.mtw?id=<mtw:out value="id" />&serviceId=<mtw:out value=".id" />">Edit</a>
 					<a href="<mtw:contextPath />/CallBlockAction.delete.mtw?id=<mtw:out value="id" />">Delete</a>
 				    </td>
 				</tr>
 			    </mtw:loop>
 			</mtw:list>
 		    </mtw:loop>
 		</mtw:list>
 


In the <a href> above (that is marked with *******), I would like to include the id of the service from the parent loop. Can anyone enlighten me?

I know that I could include the id of the service in the callBlock object, but that seems to be redundant.... Is that my option?

Thanks in advance.
 
Profile for navigo -> Messages posted by navigo [7]
Go to:   
Powered by JForum 2.1.6 © JForum Team