Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Moxy - Conditional attribute mapping?

Hi Magnus,

There's nothing specific on a DirectMapping to make it's marshal conditional. Your technique of using different accessor methods to handle the conditional behavior is a valid way of handling this. Another option, if you don't want to have those extra methods on your  object, would be to handle marshalling gmlId with a TransformationMapping. This would allow you to do the conditional checks in an external Transformer class instead.

You would just need to write a Transformer implementation something like this:

    public static class GmlIdTransformer implements FieldTransformer {
        public Object buildFieldValue(Object instance, String fieldName, Session session) {
            DefectNotificationEvent event = (DefectNotificationEvent)instance;
            if(event.getPosition() == null) {
               return null;
            }
            return event.getGmlId();
        }

       public void initialize(AbstractTransformationMapping mapping) {
        }
     }

You could use your current DirectMapping to unmarshal (by setting it read-only) and then use a write-only TransformationMapping to handle the conditional marshal:
        XMLDirectMapping gmlId = new XMLDirectMapping();
        gmlId.setAttributeName("gmlId");
        gmlId.setXPath("defect:geographicLocation/gml:Point/@gml:id");
        gmlId.setIsReadOnly(true);
        defectNotificationEvent.setPrimaryKeyFieldName("@gml:id");
        defectNotificationEvent.addMapping(gmlId);

        XMLTransformationMapping gmlIdWrite = new XMLTransformationMapping();
        gmlIdWrite.addFieldTransformer("defect:geographicLocation/gml:Point/@gml:id", new GmlIdTransformer());
        defectNotificationEvent.addMapping(gmlIdWrite);

Hope that helps,

-Matt

On 17/08/2010 9:29 AM, Magnus Heino wrote:

Hi.

        XMLDirectMapping point = new XMLDirectMapping();
        point.setAttributeName("position");
        point.setField(new XMLField("defect:geographicLocation/gml:Point/gml:pos/text()"));
        point.setConverter(new PositionConverter());
        defectNotificationEvent.addMapping(point);

        XMLDirectMapping gmlId = new XMLDirectMapping();
        gmlId.setAttributeName("gmlId");
        gmlId.setXPath("defect:geographicLocation/gml:Point/@gml:id");
        defectNotificationEvent.setPrimaryKeyFieldName("@gml:id");
        defectNotificationEvent.addMapping(gmlId);

If position is null, the first mapping generates no xml. The second mapping however, adds a gml id. But I only want it to add this gml id if position is not null. Is there a way to do this in the mapping? Right now I added a set/getGmlId where I return null is position is null, and gmlid otherwise.

--

  /Magnus Heino

_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top