Last active
September 14, 2017 09:04
-
-
Save chongma/ec924c7be88173dc03f612c467145b6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.me.kissy.sales.entities.jpa; | |
import java.io.Serializable; | |
import java.util.Date; | |
import java.util.List; | |
import javax.persistence.Column; | |
import javax.persistence.Convert; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.Lob; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
import javax.persistence.Transient; | |
import org.apache.openjpa.persistence.Externalizer; | |
import org.apache.openjpa.persistence.Factory; | |
import org.apache.openjpa.persistence.Persistent; | |
import org.apache.openjpa.persistence.jdbc.Strategy; | |
import uk.me.kissy.entities.directory.EntityMini; | |
import uk.me.kissy.entities.sales.additional.AdditionalData; | |
@Entity | |
@Table(name = "additional") | |
public class Additional implements Serializable { | |
private static final long serialVersionUID = 1L; | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private int id; | |
// @Lob | |
// private String body; | |
@Column(name = "xml") | |
// @Convert(converter = AdditionalDataConverter.class) | |
// @Externalizer("uk.me.kissy.sales.entities.jpa.AdditionalDataInterface.toString") | |
// @Factory("uk.me.kissy.sales.entities.jpa.AdditionalDataInterface.fromString") | |
@Persistent | |
@Strategy("org.apache.openjpa.xmlmapping.XmlValueHandler") | |
private AdditionalData additionalData; | |
private boolean locked; | |
@Column(length = 30) | |
private String usertemp; | |
private Date timestamp; | |
@OneToMany(mappedBy = "additional") | |
private List<Quote> quotes; | |
// @Transient | |
// private AdditionalData additionalData; | |
@Transient | |
private EntityMini user; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public boolean isLocked() { | |
return locked; | |
} | |
public void setLocked(boolean locked) { | |
this.locked = locked; | |
} | |
public String getUsertemp() { | |
return usertemp; | |
} | |
public void setUsertemp(String usertemp) { | |
this.usertemp = usertemp; | |
} | |
public Date getTimestamp() { | |
return timestamp; | |
} | |
public void setTimestamp(Date timestamp) { | |
this.timestamp = timestamp; | |
} | |
public List<Quote> getQuotes() { | |
return quotes; | |
} | |
public void setQuotes(List<Quote> quotes) { | |
this.quotes = quotes; | |
} | |
public AdditionalData getAdditionalData() { | |
return additionalData; | |
} | |
public void setAdditionalData(AdditionalData additionalData) { | |
this.additionalData = additionalData; | |
} | |
public EntityMini getUser() { | |
return user; | |
} | |
public void setUser(EntityMini user) { | |
this.user = user; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.me.kissy.entities.sales.additional; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.xml.bind.annotation.XmlRootElement; | |
@XmlRootElement | |
public class AdditionalData implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private List<AdditionalSection> additionalSection; | |
public List<AdditionalSection> getAdditionalSection() { | |
return additionalSection; | |
} | |
public void setAdditionalSection(List<AdditionalSection> additionalSection) { | |
this.additionalSection = additionalSection; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.me.kissy.sales.entities.jpa; | |
import java.io.StringReader; | |
import java.io.StringWriter; | |
import java.sql.Types; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.Unmarshaller; | |
import org.apache.openjpa.jdbc.identifier.DBIdentifier; | |
import org.apache.openjpa.jdbc.kernel.JDBCStore; | |
import org.apache.openjpa.jdbc.meta.ValueMapping; | |
import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler; | |
import org.apache.openjpa.jdbc.schema.Column; | |
import org.apache.openjpa.jdbc.schema.ColumnIO; | |
import org.apache.openjpa.meta.JavaTypes; | |
import uk.me.kissy.entities.sales.additional.AdditionalData; | |
import uk.me.kissy.external.entityBeans.UtilityDao; | |
public class AdditionalDataValueHandler extends AbstractValueHandler { | |
private static final long serialVersionUID = 1L; | |
@Override | |
public Column[] map(ValueMapping arg0, String arg1, ColumnIO arg2, boolean arg3) { | |
Column column = new Column(); | |
column.setIdentifier(DBIdentifier.newColumn(arg1)); | |
if (arg0.getType() == AdditionalData.class) { | |
column.setJavaType(JavaTypes.STRING); | |
column.setType(Types.LONGVARCHAR); | |
} | |
return new Column[] { column }; | |
} | |
@Override | |
public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) { | |
if (val == null) | |
return null; | |
if (vm.getType() == AdditionalData.class) { | |
try { | |
return marshallAdditionalData((AdditionalData) val); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
throw new IllegalStateException("only AdditionalData can be handled with this ValueHandler"); | |
} | |
@Override | |
public Object toObjectValue(ValueMapping vm, Object val) { | |
if (val == null) | |
return null; | |
if (vm.getType() == AdditionalData.class) { | |
try { | |
return unmarshallAdditionalData((String) val); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
throw new IllegalStateException("only AdditionalData can be handled with this ValueHandler"); | |
} | |
private AdditionalData unmarshallAdditionalData(String xml) throws JAXBException { | |
if (xml.startsWith("<root>")) { | |
// check whether it needs converting | |
xml = transformAdditionalOldToNew(xml); | |
} | |
JAXBContext jc = JAXBContext.newInstance(AdditionalData.class); | |
Unmarshaller marshaller = jc.createUnmarshaller(); | |
return (AdditionalData) marshaller.unmarshal(new StringReader(xml)); | |
} | |
private String marshallAdditionalData(AdditionalData additionalData) throws JAXBException { | |
JAXBContext jc = JAXBContext.newInstance(AdditionalData.class); | |
Marshaller marshaller = jc.createMarshaller(); | |
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); | |
final StringWriter w = new StringWriter(); | |
marshaller.marshal(additionalData, w); | |
String xml = w.toString(); | |
return xml; | |
} | |
private String transformAdditionalOldToNew(String xml) { | |
UtilityDao utilityDao = javax.enterprise.inject.spi.CDI.current().select(UtilityDao.class).get(); | |
// System.out.println(xml); | |
xml = utilityDao.getXMLParse(xml, "/salesXslt/convertAdditional.xsl", null, null).toString(); | |
// System.out.println(xml); | |
return xml; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment