/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.xml.jaxb;

import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;
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;

/**
 * @author 田平 create 2020年2月15日下午6:55:59
 */
public class NullPolicyEventListener extends SessionEventAdapter {
	public void preLogin(SessionEvent event) {
		Project project = event.getSession().getProject();
		for (ClassDescriptor descriptor : project.getOrderedDescriptors()) {
			for (DatabaseMapping mapping : descriptor.getMappings()) {
				if (mapping.isAbstractDirectMapping()) {
					XMLDirectMapping xmlDirectMapping = (XMLDirectMapping) mapping;
					xmlDirectMapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.EMPTY_NODE);
					xmlDirectMapping.getNullPolicy().setNullRepresentedByEmptyNode(true);
				}
			}
		}
	}
}
