
Originally Posted by
chudak
What's the locationcrudimpl class look like and what does the bean definition look like in your spring config?
Hi Chudak,
well the locationCrudImpl is like so:
PHP Code:
package is.webui;
import is.model.DataStatus;
import is.model.Location;
import java.util.Calendar;
import java.util.List;
import javax.faces.model.SelectItem;
public class LocationCrudImpl extends EntityCrudImpl<Location, Long> {
SelectItem[] locations;
public LocationCrudImpl(Class<Location> entityClass) {
super(entityClass);
}
@Override
public String create() {
entityItem.setEntryDate(Calendar.getInstance());
entityItem.setStatus(DataStatus.P);
return super.create();
}
public List<Location> getChildLocations(Location location) {
List<Location> childLocationList = dao.findListByProperty("parentLocation", location);
return childLocationList;
}
public SelectItem[] getLocations() {
return getSelectItemsByProperty("parentLocation", entityItem.getParentLocation());
}
public SelectItem[] getParentLocation() {
return locations;
}
public void setLocations(SelectItem[] location) {
this.locations = location;
}
}
EntityCrudImpl, which locationCrudimpl implements is like so:
PHP Code:
@Transactional
public class EntityCrudImpl<T, PK extends Serializable> implements
EntityCrud<T, PK> {
static Log log = LogFactory.getLog(EntityCrudImpl.class);
// This is currently being used to create a new
// instance of the entity type
Class<T> entityClass;
String outcomePrefix;
// The current entity (or "backing bean") being worked on
// wired from the applicationContext.xml file
T entityItem;
PagingState pager;
Dao<T, PK> dao;
List<T> pagedItems;
// used to convert an entity from a string to object, or
// object to a string
EntityConverter<T> converter;
// helper service
JsfService jsfService;
public EntityCrudImpl(Class<T> entityClass) {
if (entityClass == null) {
throw new IllegalArgumentException(
"entity class type required for crud");
}
if (log.isDebugEnabled()) {
log.debug("creating crud for class: " + entityClass.getName());
}
this.entityClass = entityClass;
this.outcomePrefix = entityClass.getSimpleName().toLowerCase();
pager = new PagingState();
}
public void setJsfService(JsfService jsfService) {
this.jsfService = jsfService;
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#setDao(is.dao.Dao)
*/
public void setDao(Dao<T, PK> dao) {
this.dao = dao;
}
public Dao<T, PK> getDao() {
return dao;
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#getPager()
*/
public PagingState getPager() {
if (pager.getTotalCount() < 0) {
pager.setTotalCount(dao.getTotalCount());
}
return pager;
}
/**
* Create a new instance of the entity type
* @return the new object instance
*/
protected T newInstance() {
try {
return entityClass.newInstance();
} catch (Exception e) {
throw new FacesException("failed to instantiate entity "
+ entityClass.getName(), e);
}
}
/**
*
* @param resetPaging
*/
protected void reset(boolean resetPaging) {
entityItem = null;
pager.setTotalCount(-1);
pagedItems = null;
if (resetPaging) {
pager.setPageIndex(0);
}
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#listSetup()
*/
public String listSetup() {
reset(true);
return outcomePrefix + "_list";
}
/**
*
*/
public void validateCreate(FacesContext facesContext,
UIComponent component, Object value) {
T newItem = null;
try {
newItem = entityClass.newInstance();
} catch (Exception e) {
log.error("failed to create item instance", e);
}
String newItemString = converter.getAsString(FacesContext.getCurrentInstance(), null, newItem);
String itemString = converter.getAsString(FacesContext.getCurrentInstance(), null, entityItem);
if (!newItemString.equals(itemString)) {
createSetup();
}
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#createSetup()
*/
public String createSetup() {
reset(false);
entityItem = newInstance();
return outcomePrefix + "_create";
}
/**
* Persist the current entity item to the database
*/
public String create() {
try {
dao.create(entityItem);
jsfService.addMessage("Item was successfully created.");
} catch (Exception e) {
jsfService.addError(e.getMessage());
return null;
}
return listSetup();
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#detailSetup()
*/
public String detailSetup() {
return scalarSetup(outcomePrefix + "_detail");
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#editSetup()
*/
public String editSetup() {
return scalarSetup(outcomePrefix + "_edit");
}
/**
* Update a persisted entity
*/
public String edit() {
// convert the current entity item to a string
String itemString = converter.getAsString(FacesContext.getCurrentInstance(), null, entityItem);
String itemId = jsfService.getReqParam("itemId");
// verify the edited item is valid
if (itemString == null || itemString.length() == 0
|| !itemString.equals(itemId)) {
String outcome = editSetup();
if ((outcomePrefix + "_edit").equals(outcome)) {
jsfService.addError("Could not edit item. Try again.");
}
return outcome;
}
// attempt to update
try {
dao.update(entityItem);
jsfService.addMessage("Item was successfully updated.");
} catch (Exception e) {
jsfService.addError(e.getMessage());
return null;
}
return detailSetup();
}
/**
* Remove/delete an entity from persistence
*/
public String delete() {
T persistentObject = converter.getAsObject(FacesContext.getCurrentInstance(), null, jsfService.getReqParam("itemId"));
try {
dao.delete(persistentObject);
jsfService.addMessage("Item was successfully deleted.");
} catch (Exception e) {
jsfService.addError(e.getMessage());
return null;
}
return listSetup();
}
@SuppressWarnings("unchecked")
protected String scalarSetup(String outcome) {
reset(false);
entityItem = (T) jsfService.getObjViaReqParam("itemId", converter, null);
if (entityItem == null) {
String itemId = jsfService.getReqParam("itemId");
jsfService.addError("The item with id " + itemId
+ " no longer exists.");
}
return outcome;
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#next()
*/
public String next() {
reset(false);
getPager().nextPage();
return outcomePrefix + "_list";
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#prev()
*/
public String prev() {
reset(false);
getPager().previousPage();
return outcomePrefix + "list";
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#getItem()
*/
public T getItem() {
if (entityItem == null) {
entityItem = newInstance();
}
return entityItem;
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#getPagedItems()
*/
public List<T> getPagedItems() {
if (pagedItems == null) {
pagedItems = dao.findPaged(pager.getPageIncrement(), pager.getPageIndex());
}
return pagedItems;
}
public SelectItem[] getSelectItems() {
return jsfService.getSelectItems(dao.findAll());
}
/*
* Return SelectItem for a subset of enitities depending on the value of a
* property being searched on
*/
public SelectItem[] getSelectItemsByProperty(String propertyName, Object value) {
return jsfService.getSelectItems(dao.findListByProperty(propertyName, value));
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#setConverter(javax.faces.convert.Converter)
*/
public void setConverter(EntityConverter<T> converter) {
this.converter = converter;
}
/*
* (non-Javadoc)
*
* @see is.webui.EntityCrud#getConverter()
*/
public EntityConverter<T> getConverter() {
return converter;
}
public void setItem(T entityItem) {
this.entityItem = entityItem;
}
}
And the bean declaration is like so:
PHP Code:
...
<bean id="locationVO" class="is.vo.LocationVO">
<property name="locationCrudImpl" ref="locationCrud" />
</bean>
...
Thanks, hope this helps