Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » [MOXy] Mapping element to boolean field
[MOXy] Mapping element to boolean field [message #504386] Fri, 18 December 2009 15:45 Go to next message
nikkorv is currently offline nikkorvFriend
Messages: 4
Registered: July 2009
Junior Member
Hi,
I'm having trouble doing the following ox-mapping:

From xml:
<MyObject>
   <BooleanValue />
</MyObject>


To this domain object:

class MyObject {
   private boolean booleanValue;
}


The rules for the mapping is that if the element <BooleanValue /> exist in xml the value of the corresponding field should be set to true, else the value of field booleanValue should be set to false. The reverse logic is also true: booleanValue=true should produce the element and booleanValue=false should omit outputting the element.

I have no clue how to create descriptors for this kind of mapping. Any help is appreciated.
Re: [MOXy] Mapping element to boolean field [message #504427 is a reply to message #504386] Fri, 18 December 2009 21:33 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

You caught me on the way out the door for the holidays. The following is close to what you could do:

import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.oxm.XMLDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.NullPolicy;
import org.eclipse.persistence.sessions.Project;
import org.eclipse.persistence.sessions.Session;

public class CustomerProject extends Project {

    public CustomerProject() {
        this.addDescriptor(getCustomerDescriptor());
    }

    private XMLDescriptor getCustomerDescriptor() {
        XMLDescriptor xmlDescriptor = new XMLDescriptor();
        xmlDescriptor.setJavaClass(Customer.class);
        xmlDescriptor.setDefaultRootElement("customer");

        XMLDirectMapping booleanPropertyMapping = new XMLDirectMapping();
        booleanPropertyMapping.setAttributeName("fullTime");
        booleanPropertyMapping.setXPath("full-time");

        NullPolicy nullPolicy = new NullPolicy();
        nullPolicy.setSetPerformedForAbsentNode(false);
        booleanPropertyMapping.setNullPolicy(nullPolicy);

        booleanPropertyMapping.setConverter(new BooleanConverter());
        xmlDescriptor.addMapping(booleanPropertyMapping);

        return xmlDescriptor;
    }

    private static class BooleanConverter implements Converter {

        public Object convertDataValueToObjectValue(Object dataValue, Session session) {
            return true;
        }

        public Object convertObjectValueToDataValue(Object objectValue, Session session) {
            if(objectValue.equals(true)) {
                return "";
            } else {
                return null;
            }
        }

        public void initialize(DatabaseMapping mapping, Session session) {
            // TODO Auto-generated method stub
            
        }

        public boolean isMutable() {
            // TODO Auto-generated method stub
            return false;
        }
        
    }
}


-Blaise
Re: [MOXy] Mapping element to boolean field [message #504438 is a reply to message #504427] Sat, 19 December 2009 03:42 Go to previous message
nikkorv is currently offline nikkorvFriend
Messages: 4
Registered: July 2009
Junior Member
Thanks for the example, it helped a lot. I have now managed to create a working descriptor for mapping my example object. There is just one annoying thing left though. When marshaling from java to xml i get the following output:

<MyObject>
   <booleanValue></booleanValue>
</MyObject>


It would be great if the empty booleanVaue-element could be outputted with a trailing slash instead:

<MyObject>
   <booleanValue />
</MyObject>


I bet there is an easy way to achieve this, I just cant find it in the documentation. Any suggestions?
Previous Topic:Webstart much slower since EL 2.0
Next Topic:Dates: Column Integer X Property Calendar
Goto Forum:
  


Current Time: Sat May 11 07:59:12 GMT 2024

Powered by FUDForum. Page generated in 0.02800 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top