package au.gov.ga.geodesy.support.marshalling.moxy;

import java.lang.reflect.Method;

import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy;
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
import org.eclipse.persistence.sessions.Project;
import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;

/**
 * Ensure null values are marshalled as absent, rather than empty, XML elements.
 * Empty XML elements will often break schema validation.
 */
public class NullPolicySessionEventListener extends SessionEventAdapter {

    @Override
    public void preLogin(SessionEvent event) {
        Project project = event.getSession().getProject();
        for (ClassDescriptor descriptor : project.getOrderedDescriptors()) {
            for (DatabaseMapping mapping : descriptor.getMappings()) {
                // TODO: keep this around for a while
                /* if (mapping.isAbstractDirectMapping()) { */
                /*     XMLDirectMapping m = (XMLDirectMapping) mapping; */
                /*     m.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.ABSENT_NODE); */
                /* } */
                /* else if (mapping.isAbstractCompositeObjectMapping()) { */
                /*     XMLCompositeObjectMapping m = (XMLCompositeObjectMapping) mapping; */
                /*     m.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.ABSENT_NODE); */
                /* } */
                /* else if (mapping.isAbstractCompositeCollectionMapping()) { */
                /*     XMLCompositeCollectionMapping m = (XMLCompositeCollectionMapping) mapping; */
                /*     m.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.ABSENT_NODE); */
                /* } */
                /* else if (mapping.isAbstractCompositeDirectCollectionMapping()) { */
                /*     XMLCompositeDirectCollectionMapping m = (XMLCompositeDirectCollectionMapping) mapping; */
                /*     m.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.ABSENT_NODE); */
                /* } */
                try {
                    Method getNullPolicy = mapping.getClass().getMethod("getNullPolicy");
                    AbstractNullPolicy nullPolicy = (AbstractNullPolicy) getNullPolicy.invoke(mapping);
                    nullPolicy.setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
                }
                catch (NoSuchMethodException ok) {
                }
                catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
     }
}

@@ -50,365 +50,8 @@
 y;%0A%0A
-import java.lang.reflect.Method;%0A%0Aimport org.eclipse.persistence.descriptors.ClassDescriptor;%0Aimport org.eclipse.persistence.mappings.DatabaseMapping;%0Aimport org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy;%0Aimport org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;%0Aimport org.eclipse.persistence.sessions.Project;%0A
 impo
@@ -100,16 +100,16 @@
 nEvent;%0A
+
 import o
@@ -452,16 +452,19 @@
 %0A       
+ //
  Project
@@ -510,24 +510,27 @@
 t();%0A       
+ //
  for (ClassD
@@ -590,24 +590,27 @@
 )) %7B%0A       
+ //
      for (Da
@@ -1968,16 +1968,19 @@
         
+ //
  try %7B%0A 
@@ -1989,24 +1989,27 @@
             
+ //
      Method 
@@ -2082,24 +2082,27 @@
             
+ //
      Abstrac
@@ -2189,24 +2189,27 @@
             
+ //
      nullPol
@@ -2284,32 +2284,35 @@
 %0A               
+ //
  %7D%0A             
@@ -2305,32 +2305,35 @@
 %0A               
+ //
  catch (NoSuchMe
@@ -2359,32 +2359,35 @@
 %0A               
+ //
  %7D%0A             
@@ -2388,16 +2388,19 @@
         
+ //
  catch (
@@ -2425,24 +2425,27 @@
             
+ //
      throw n
@@ -2475,32 +2475,35 @@
 %0A               
+ //
  %7D%0A            %7D
@@ -2496,24 +2496,27 @@
 %0A           
+ //
  %7D%0A        %7D
@@ -2513,16 +2513,19 @@
 %0A       
+ //
  %7D%0A     

Disable xsi:nil null policy