0

Disclaimer: I'm new to JSF but still couldn't find any other questions to help me with this.

I have a CRUD view in JSF(More specifically in a primefaces dialog if that's important) when I have the ID as a long it works fine, but when I use ObjectID as the ID(I use ObjectID because Morphia will auto-generate an ID if using ObjectID).

What happens is the ObjectID transfers down to the view and displays fine, but when I click save nothing happens(Breakpoint doesn't get hit in save method and no errors are displayed in the console) as mentioned before if I use a long as an ID this works fine.

Here is the input field of the ObjectID and the action for the save button(I display the ObjectID as the save method returns with a null ObjectID and just creates a new record if I don't).

<p:inputText value="#{dealerBean.selectedDealer.id}"
    style="display:none;"  />

<p:commandButton id="saveButton" value="Save" update="dealers"
    actionListener="#{dealerBean.save}"
    title="Save" type="Submit" oncomplete="dealerDialog.hide()">
</p:commandButton>

//type of selectedDealer
@com.google.code.morphia.annotations.Entity
class Dealer {
    @com.google.code.morphia.annotations.Id
    @Property //Generates getters and setter in XTend
    org.bson.types.ObjectId id;
}

My guess is that it's having trouble de-serializing ObjectID(No error message in console though) but I don't know JSF well enough to know where to go from there and any help would be appreciated.

Zergleb
  • 2,212
  • 15
  • 24
  • You're experiencing a conversion error. This is most likely because of a type mismatch between the type you bound in the backing bean of your page and the actual type of `selectedDealer.id`. What is the type of that variable? – kolossus Dec 01 '12 at 19:29
  • The type is org.bson.types.ObjectId from what I understand using this type helps MongoDB do auto-generate id's. I'll update my above comment . – Zergleb Dec 01 '12 at 23:59
  • Doesn't work that way in JSF-town. You have to explicitly declare the conversion semantics for a custom type if it's anything other than a String or primitive. See [this answer](http://stackoverflow.com/a/13189326/1530938) – kolossus Dec 02 '12 at 01:18

1 Answers1

0

Thanks to kolossus for the answer in the edits above. This is the code I used(Sorry it's in groovy rather than Java)(I switched from Xtend to groovy because of issues with XTend and validation)

@FacesConverter("objectId")
class ObjectIdConverter implements Converter {
    def Object getAsObject( FacesContext context, UIComponent component,
                               String value) {
        if(value == null || "".equals(value) || value.size() < 10) {
            return null
        }
        return new ObjectId(value)
    }

    def String getAsString(FacesContext context, UIComponent component,
                               Object value) {
        if(value == null){
            return null
        }
        if (value instanceof ObjectId) {
            return (value as ObjectId).toString()
        } else {
            throw new IllegalArgumentException( "Cannot convert "+ value +" in       KeyConverter" );
        }
    }
}
Zergleb
  • 2,212
  • 15
  • 24