Created
October 15, 2012 09:45
-
-
Save asicfr/3891735 to your computer and use it in GitHub Desktop.
struts2RestJpaBootstrap - generic action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.demo.action; | |
import java.util.List; | |
import org.demo.business.service.IServices; | |
import com.opensymphony.xwork2.ActionSupport; | |
import com.opensymphony.xwork2.util.logging.Logger; | |
import com.opensymphony.xwork2.util.logging.LoggerFactory; | |
public abstract class GenericAction<T, PK> extends ActionSupport | |
{ | |
private static final long serialVersionUID = 1L; | |
public static final String RESULT_FORM = "form" ; | |
public static final String RESULT_LIST = "list" ; | |
protected final Logger LOG = LoggerFactory.getLogger(GenericAction.class); | |
protected T current = null ; | |
protected PK restid = null; | |
protected List<T> searchResult = null ; | |
/** | |
* @return | |
*/ | |
protected abstract IServices<T, PK> getServices(); | |
/** | |
* @return | |
* @throws Exception | |
*/ | |
public String load() throws Exception { | |
if (LOG.isDebugEnabled()) LOG.debug("Method 'load'"); | |
innerload(); | |
return RESULT_FORM ; | |
} | |
/** | |
* @return | |
* @throws Exception | |
*/ | |
public String save() throws Exception { | |
if (LOG.isDebugEnabled()) { | |
LOG.debug("Method 'save'"); | |
LOG.debug("Form values : " + current ); | |
} | |
if (current != null) { | |
getServices().save(current); | |
addActionMessage(getText("entity.save")); | |
} else { | |
addActionError(getText("entity.nothing.to.save")); | |
} | |
return RESULT_FORM ; | |
} | |
/** | |
* @return | |
* @throws Exception | |
*/ | |
public String search() throws Exception { | |
if (LOG.isDebugEnabled()) { | |
LOG.debug("Method 'search'"); | |
LOG.debug("Form values : " + current ); | |
} | |
searchResult = getServices().search( current ); | |
if (LOG.isDebugEnabled()) LOG.debug("After SEARCH : count = " + searchResult.size() ); | |
if ( searchResult.size() > 1 ) { | |
final String[] args = {Integer.toString(searchResult.size())}; | |
final String text = getText("entity.found.many", args); | |
addActionMessage(text); | |
return RESULT_LIST ; // Go to the list page | |
} | |
else if ( searchResult.size() == 1 ) { | |
current = searchResult.get(0); | |
addActionMessage(getText("entity.found.one")); | |
return RESULT_FORM ; // Go to the list page | |
} | |
else { | |
addActionMessage(getText("entity.nothing.found")); | |
return RESULT_FORM ; // Stay on the form page | |
} | |
} | |
/** | |
* @return | |
* @throws Exception | |
*/ | |
public String delete() throws Exception { | |
if (LOG.isDebugEnabled()) { | |
LOG.debug("Method 'delete'"); | |
LOG.debug("Id send : " + restid ); | |
} | |
getServices().delete(restid); | |
return RESULT_FORM ; | |
} | |
/** | |
* TODO est-ce util ? | |
*/ | |
public String clear() { | |
if (LOG.isDebugEnabled()) LOG.debug("Method 'clear'"); | |
this.current = null; | |
this.restid = null; | |
this.searchResult = null; | |
return RESULT_FORM ; // Stay on the form page | |
} | |
/** | |
* @throws Exception | |
*/ | |
protected void innerload() throws Exception { | |
if (LOG.isDebugEnabled()) LOG.debug("ID is valid : try to LOAD ..."); | |
current = getServices().load(restid); | |
if ( current != null ) { | |
addActionMessage(getText("entity.found")); | |
} | |
else { | |
addActionError(getText("entity.not.found")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment