08-26-2007 10:52 AM
08-26-2007 03:46 PM
08-27-2007 07:35 AM
08-28-2007 11:21 AM
08-29-2007 02:34 AM
08-29-2007 03:40 AM
08-31-2007 12:58 AM
09-19-2007 12:11 PM
09-19-2007 08:36 PM
09-23-2007 10:39 AM
<h:commandButton image="/images/icons/up.gif" action="#{AddCategoryBean.getLevelUpChildrenForNode}" rendered="#{AddCategoryBean.isParentListNotEmpty}" immediate="true">
</h:commandButton>
<h:dataTable id="categoriesTable" border="0" binding="#{AddCategoryBean.categoriesTable}" value="#{AddCategoryBean.categoryItems}" var="line">
<h:column>
<h:selectBooleanCheckbox value="#{line.selected}" id="categoryCheckbox" immediate="true"/>
</h:column>
<h:column>
<h:commandLink id="cmd-link" action="#{AddCategoryBean.getChildrenForNode}" immediate="true">
<h:outputText id="output-text" value="#{line.localName}"/>
</h:commandLink>
</h:column>
</h:dataTable>public class AddCategoryBean extends BaseDetailsBean
{
private FacesContext context = FacesContext.getCurrentInstance();
private List<NodeRef> categories;
private Stack<NodeRef> parentCategories;
private ArrayList<SelectItem> rootCategories;
private Collection<ChildAssociationRef> childRefs;
private List<AddCategoryItem> categoryItems;
private UIData categoriesTable;
private static final String MSG_ERROR_UPDATE_CATEGORY = "error_update_category";
//default constructor
public AddCategoryBean()
{
super();
categories = (List<NodeRef>)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("savedCategories");
if(categories == null)
categories = new ArrayList<NodeRef>();
parentCategories = new Stack<NodeRef>();
categoryItems = new ArrayList<AddCategoryItem>();
getRootChildren(context);
}
/**
* get top level categories
*/
private void getRootChildren(FacesContext context)
{
childRefs = getCategoryService().getCategories(Repository.getStoreRef(), ContentModel.ASPECT_GEN_CLASSIFIABLE, Depth.IMMEDIATE);
updateTableModel();
}
/**
* get categories by nodeRef
* the nodeRef comes from the selected row
*/
public String getChildrenForNode()
{
ChildAssociationRef assocRef = ((AddCategoryItem)categoriesTable.getRowData()).getChildRef();
NodeRef nodeRef = new NodeRef(Repository.getStoreRef(), assocRef.getChildRef().getId());
childRefs = getCategoryService().getChildren(nodeRef,
CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE);
addCheckedRows();
updateTableModel();
ChildAssociationRef parentRef = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getNodeService().getPrimaryParent(nodeRef);
parentCategories.push(parentRef.getParentRef());
UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
return "success";
}
/**
* get categories by nodeRef.
* the nodeRef comes from the stack that holds the parent reference.
* this method is used to navigate one level up.
*/
public String getLevelUpChildrenForNode()
{
if ( !parentCategories.isEmpty())
{
NodeRef parentRef = (NodeRef)this.parentCategories.pop();
childRefs = getCategoryService().getChildren(parentRef,
CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE);
addCheckedRows();
updateTableModel();
UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans();
}
return "success";
}
/**
* update table model with new values
*/
public void updateTableModel()
{
categoryItems.clear();
AddCategoryItem categoryItem = null;
for(ChildAssociationRef ref: childRefs)
{
categoryItem = new AddCategoryItem();
categoryItem.setChildRef(ref);
if(categories.contains(ref.getChildRef()))
{
categoryItem.setSelected(true);
}
categoryItems.add(categoryItem);
}
}
/**
* Use Spring JSF integration to return the category service bean instance
*
* @param context FacesContext
* @return category service bean instance or throws runtime exception if not found
*/
private CategoryService getCategoryService()
{
CategoryService service = Repository.getServiceRegistry(context).getCategoryService();
if (service == null)
{
throw new IllegalStateException("Unable to obtain CategoryService bean reference.");
}
return service;
}
/**
* add selected rows to the "categories" collection
* the method will remove rows that have been disselected from the "categories" collection!
*/
private void addCheckedRows()
{
for (int i=0; i < categoriesTable.getRowCount(); i++)
{
categoriesTable.setRowIndex(i);
UISelectBoolean box = (UISelectBoolean)categoriesTable.findComponent("categoryCheckbox");
boolean isSelected = box.isSelected();
ChildAssociationRef assocRef = ((AddCategoryItem)categoriesTable.getRowData()).getChildRef();
NodeRef currentRowRef = assocRef.getChildRef();
if((categories.contains(currentRowRef)) && !(isSelected))
{
categories.remove(currentRowRef);
}
if((isSelected) && !(categories.contains(currentRowRef)))
{
categories.add(currentRowRef);
}
}
}
/**
* Updates the categories for the current document
*
* @return The outcome
*/
public String saveCategories()
{
String outcome = "cancel";
UserTransaction tx = null;
addCheckedRows();
try
{
tx = Repository.getUserTransaction(FacesContext.getCurrentInstance());
tx.begin();
NodeService nodeService = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getNodeService();
// firstly retrieve all the properties for the current node
Map<QName, Serializable> updateProps = nodeService.getProperties(
getSpace().getNodeRef());
// create a node ref representation of the selected id and set the new properties
updateProps.put(ContentModel.PROP_CATEGORIES, (Serializable)this.categories);
// set the properties on the node
this.nodeService.setProperties(getSpace().getNodeRef(), updateProps);
// commit the transaction
tx.commit();
// reset the state of the current document so it reflects the changes just made
getSpace().reset();
outcome = "finish";
}
catch (Throwable e)
{
try { if (tx != null) {tx.rollback();} } catch (Exception ex) {}
Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
FacesContext.getCurrentInstance(), MSG_ERROR_UPDATE_CATEGORY), e.getMessage()), e);
}
return outcome;
}
}public class AddCategoryItem
{
private ChildAssociationRef childRef;
private boolean selected = false;
public ChildAssociationRef getChildRef() {
return childRef;
}
public void setChildRef(ChildAssociationRef childRef) {
this.childRef = childRef;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getLocalName()
{
if(childRef != null)
{
return childRef.getQName().getLocalName();
}
return "";
}
}Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.