2
votes

I am working on my first Notes/XPages/Java application and I am stuck at some of the basic 'crud' level. The following is part of the managed bean. I can load the data on to the XPage, but saving the Checkbox field is causing me problems, i.e. it won't save. I assume it is to do with the data type as the CheckboxGroup is multivalued.

Form Fields are: Category Employment Role

Variables

public class TrainingModule implements Serializable {
private String Category;
private Object EmploymentRole;

public String getCategory() {
return Category; }

public void setCategory(final String category) {
Category = category;}

public Object getEmploymentRole() {
return EmploymentRole;}

public void setEmploymentRole(final Object employmentRole) {
EmploymentRole = employmentRole;}

Load Method

public void load(final String unid) {
    setUnid(unid);
    Document doc = null;
    try {
        doc = ExtLibUtil.getCurrentDatabase().getDocumentByUNID(getUnid());
        setCategory(doc.getItemValueString("Category"));
        setEmploymentRole(doc.getItemValue("EmploymentRole"));
etc

Save Method

public boolean saveData() {
    boolean result = false;
    Document doc = null;

    try {

        doc.replaceItemValue("Category", Category);
        doc.replaceItemValue("EmploymentRole", EmploymentRole);

        result = doc.save()

etc

XPage

<xp:checkBoxGroup id="checkBoxGroup1" 
value="#{TrainingModule.employmentRole}">
<xp:selectItem itemLabel="Admin" itemValue="Admin">
</xp:selectItem>
<xp:selectItem itemLabel="Installation" itemValue="Installation">
</xp:selectItem>
<xp:selectItem itemLabel="Proj Man" itemValue="Proj Man">
</xp:selectItem>
</xp:checkBoxGroup>

I know there are similar postings, but I just can't seem to relate them to what I am trying to achieve.

My next task will be using upload and download controls with Java so any hints or traps to avoid would be great. Any help would be appreciated.

2
I assume you intentionally left out the line in your save method that sets the doc object? If not then you should instantiate your doc object, too, as it now is set to null so the code will not be executed and instead it falls into the catch block.Oliver Busse
@OliverBusse Yes sorry Oliver, I was trying not to put too much code - a mistake. doc = ExtLibUtil.getCurrentDatabase().getDocumentByUNID( getUnid());Mark Maden

2 Answers

2
votes

Define your employment roles as a field of type ArrayList<String>:

private List<String> employmentRoles = new ArrayList<String>();

public void setEmploymentRoles(List<String> employmentRoles) {
    this.employmentRoles = employmentRoles;
}

public List<String> getEmploymentRoles() {
    return employmentRoles;
}

Read the values with

setEmploymentRoles(doc.getItemValue("EmploymentRole"));

and save the values with

doc.replaceItemValue("EmploymentRole", new Vector(getEmploymentRoles()));

Btw, you shouldn't start a field name with a capital letter. Look here for Java naming conventions.

0
votes

Since you need to load/save your data, you might be better off with an object data source. Anyway try this:

public Object[] getEmploymentRole() {
return EmploymentRole;}

public void setEmploymentRole(final Object[] employmentRole) {
EmploymentRole = employmentRole;}

An array can't be cast to an Object and a checkboxgroup tries to get/set an array.

This then leads to a slight change in your save method:

    doc.replaceItemValue("Category", Category);
    Vector v = new Vector(Arrays.asList(EmploymentRole));
    doc.replaceItemValue("EmploymentRole", v);

Let us know how it goes