12/21/2010

Fire onchange event on IE





Typical way to make 'enter' key working on certain field is


<rich:hotKey
key="return"
handler="#{rich:element('clearance_agent_id')}.onchange()"
selector="#land_lto_id" />


However, this doesn't work on IE.

To make it work you can add a trick.


<rich:hotKey
key="return"
handler="#{rich:element('clearance_agent_id')}.focus();#{rich:element('clearance_agent_id')}.onchange()"
selector="#land_lto_id" />


11/15/2010

How to get OracleConnection from T4CConnection or Proxy


java.sql.Connection t4Connection = BorrowedConnectionProxy.getWrappedConnection(conn);
OracleConnection oracleConn = (OracleConnection) t4Connection;
context = new DefaultContext((OracleConnection) oracleConn);

10/21/2010

Seam: Manual Transaction



1. start a nested conversation and set em flush mode to manual before showing popup.

@Begin(nested = true, flushMode = FlushModeType.MANUAL)
public void prepareEdit() {}

2. when click save button, end conversation and flush em.

@End
public void saveEdit() {
xxxService.update(WhatEverObjectYouAreEditing);
dbHelperService.flush();

// If the current object needs to updated on session scope, raise event
Events.instance().raiseEvent(eventName, WhatEverObjectYouAreEditing)

// Then the parent action should listen to the eventName, so the object can be reloaded.
}

3. when click cancel button, end conversation and refresh data

@End
public void cancel() {
dbHelperService.refresh(WhatEverObjectYouAreEditing);
}

Couple of things to remember
1. Conversation vs Transaction
- Transaction is not started when conversation starts
- You can only specify transaction will be maintained manually when conversation starts. (That definition will be cleared when conversation ends)
- Transaction will be committed when you execute.
- When transaction ends, dirty object will be cleared if not commited.

2. Flush vs Commit
- When executing a method, you can flush any time.
- Flush updates database to reflect the changes in objects in memory.
: However, it is still not committed yet.
: Unless flushes, database will not see the change.

- When the execution ends, transaction will be commited automatically.
- You can run flush, but cannot run commit




Create Textarea with maxlength validation



<script type="text/javascript">
function textCounter(fieldstr, countfieldstr, maxlimit){
field = document.getElementById(fieldstr);
countfield = document.getElementById(countfieldstr);

if (field.value.length > maxlimit) {
// if too long, trim it!
field.value = field.value.substring(0, maxlimit);
} else {
// update 'characters left' counter
countfield.value = maxlimit - field.value.length;
}
}
</script>

<h:form id="myform">
<h:inputTextarea id="myarea"
onkeydown="textCounter('myform:myarea','myform:myarea_remline',100);"
onkeyup="textCounter('myform:myarea','myform:myarea_remline',100);"
onfocus="textCounter('myform:myarea','myform:myarea_remline',100);"
disabled="#{disabled}"/>
<br/>
<h:inputText id="myarea_remline" value="100" size="4" maxlength="4" disabled="true"/> characters remaining
</h:form>

s:link, s:button excution order on action and view attribute


s:button and s:link can have both 'action' and 'view' attributes.

When both are given, it will try to excute 'action' first, then 'view' next.
However, if action returns something, it will be forwarded to the 'something'
If, action returns nothing, it will proceed to 'view'.



rule

  • 1. execute action

  • 1.1 if action returns -> redirect to the page

  • 1.2 if action doesn't return -> try to call 'view'

8/12/2010

How to call database store procedure with return parameter from EJB3


@Resource(mappedName = "java:/myDatasource")
private DataSource ds;

public void callSetLoginUserId(String userId) throws Exception {
Connection conn = null;
CallableStatement st = null;
try {
String sql = "{ ? = call my_pkg.create_user(?) }";
conn = ds.getConnection();
st = conn.prepareCall(sql);
st.registerOutParameter(1, Types.BIGINT);
st.setString(2, "username");
st.execute();
Long userId = st.getLong(1);
}
catch(Exception e) {
log.error("calling my_pkg.create_user has failed", e);
throw e;
}
finally {
try {
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// ignore
}
}