zwwcn

Just another WordPress.com site

Monthly Archives: June 2011

invalid value in selectitems when we refresh the selectitems

here’s the scenario: there’s a selectonemenu on the page, which dynamically load items based on the customer’s selection on another drop down list on the same page. problem is that after we loading new items for this selectonemenu, we can see them but trying to save will give error “invalid value”.

 

The new selected item can’t be found in the latest selected items. This is classic hashCode(), equals() issue, we just need to overwrite the equals method, and let two entity with the same value equal to each other.

e.g: LeadJobDetails.xhtml vet setting

 

rich picklist

<rich:pickList value="#{pickBean.listValues}">
<f:selectItem itemValue="Bentley" itemLabel="Bentley"/>
<f:selectItem itemValue="Audi" itemLabel="Audi"/>
<f:selectItems value="#{pickBean.sourceList}"/>
</rich:pickList>


listValues should be list of string
sourceList should be list of selectitem

Sample codes:
selectedFranchise.getFranchiseAccountCategories().clear();
for(String accountCatgoryName : selectedFranchiseAccountCategoryList){
for (AccountCategory tempAccountCategory : getAccountCategoryList()){
if (tempAccountCategory.getAccountCategoryName().equalsIgnoreCase(accountCatgoryName)){
selectedFranchise.getFranchiseAccountCategories().add(tempAccountCategory);
}
}
}
em.merge(selectedFranchise);
em.flush();

RuntimeException vs Exception

Generally RuntimeExceptions are exceptions that can be prevented programmatically. E.g NullPointerException, ArrayIndexOutOfBoundException. If you check for null before calling any method, NullPointerException would never occur. Similarly ArrayIndexOutOfBoundException would never occur if you check the index first. RuntimeException are not checked by the compiler, so it is clean code.

 

We want to catch the exception and record it but don’t want to show user the error page when error happens. so what we could do is to throw a runtime exception when we catch the exception.

public void test throw customizedRuntimeException(){

try{

somecode which generates error

}catch(IOException e){

throw new customizedRuntimeException(“error msg goes here”);

}

}

then we can catch the customizedRuntimeException:

 

try{

test();

}catch(customizedRuntimeException){

log.error(“some more info”);

}

 

WebRemote and Seam

Step 1: add webremote annotation for the specific method in local interface

@Local
public interface ProspectController extends StatefulController {

@WebRemote
public String createNewProspect();

}

Step 2:  create the javascript function

function showProspectPopUp(modelPanel,id){
Seam.Remoting.getContext().setConversationId(id);   //this is important: make sure the remote call is in the same conversation
Seam.Component.getInstance(“prospectController”).createNewProspect(null,exceptionHandler);
Richfaces.showModalPanel(modelPanel);
}

Step 3:  in jsf page, add the following:

<ui:define name=”js”>
<s:remote include=”prospectController”/>
<onepl:javascript src=”/js/main.js” cachebusting=”true”/>   //where the javascript function locates
</ui:define>

<h:outputLink onclick=”showProspectPopUp(‘prospPanel’,#{conversation.id});return false;”  value=””>test </h:outputLink>

 

Fixed column width and text overflow

table
{
table-layout:fixed; //will fix the width, may not necessary
}

</code>
p.test {
	white-space: nowrap;         //all text go to one line
	width: 100%;
	overflow: hidden;            //extra text will be hiden
	text-overflow: ellipsis;     //three periods append to the text
}

set width for rich:datatable

<rich:dataTable id="itemsTable" style="width:930px;" value="#{scheduleItems}">

<rich:column id="addressCol" styleClass="#{scheduleItem.invoice.quote ? 'highlight' : '' }" style="width:120px;">
<code>

a4j hide/show button


<c:if test="#{inv.quote}">
<div>
<h:outputLabel styleClass="medium inline" id="leadStatusLabel" value="Change Status to" for="leadStatus"/>
<h:selectOneMenu id="leadStatus" value="#{leadStatusHome.leadStatus}" disabled="#{!statusChangeable}">
<f:selectItems  value="#{leadStatusHome.statusList}"/>
<a4j:support reRender="saveAndCompletePanel,saveAndComplete,savePanel,save" event="onchange" ajaxSingle="true" limitToList="true"></a4j:support>
</h:selectOneMenu>
</div>
</c:if>








<a4j:outputPanel id="saveAndCompletePanel">
<fieldset>
<div>
<h:commandButton  id="saveAndComplete" action="#{actionBean[saveAndCompletion]}" value="Save and Completed"  rendered="#{inv.quote and inv.scheduleItem != null and !hideScheduleItem and not(leadStatusHome.leadStatus=='CO' or leadStatusHome.leadStatus=='PD')}" styleClass="btn alignright btn_green"/>
</div>
</fieldset>
</a4j:outputPanel>




 

 

em.refresh vs em.find

use em.refresh  if entity is managed, otherwise use em.find


if (em.contains(franchiseCustomer)){
em.refresh(franchiseCustomer);
}else{
franchiseCustomer = em.find(Customer.class,franchiseCustomer.getObjID());
}

Hibernate Restrictions.disjunction()

More generally a disjunction is a logical formula that can have one or more literals separated only by ORs

 

<pre><a id="querycriteria-narrowing">List cats = sess.createCriteria(Cat.class) .
add( Restrictions.in( "name", new String[] { "Fritz", "Izi", "Pk" } ) )
.add( Restrictions.disjunction()
.add( Restrictions.isNull("age") )
.add( Restrictions.eq("age", new Integer(0) ) )
.add( Restrictions.eq("age", new Integer(1) ) )
.add( Restrictions.eq("age", new Integer(2) ) ) ) ) .list();</a>