How to Use NoSql Postgres with Spring Boot - postgresql

in a post http://blog.endpoint.com/2013/06/postgresql-as-nosql-with-data-validation.html I learnt some basic things about postgres' nosql feature. I'm still wondering about how to use this feature in Spring boot. Is there any documents on this? Thank you so much!

We wrote 4 Java classes to support noSQL with postgreSQL in our Spring project.
JsonString.java is our central class to support noSQL.
JsonStringDbMapper.java maps our JsonString in Hibernate to JDBC type
PGobject(type="jsonb"). This type is defined in postgreSQL JDBC driver.
JsonbH2Dialect.java maps PGobject to the embedded H2 database which we are using in our JUnit tests only. We define this hibernate dialect in our application-test.yml.
JsonStringDeserializer.java to use Jackson in our REST services. The JsonString raw value is integrated in the Beans which uses JsonString.
In our entity classes we are using the type JsonString like:
#Type(type = "de.project.config.JsonStringDbMapper")
private JsonString jsonData = new JsonString(null);
JsonString has methods to read and write any Java beans using Jackson.
JsonString maps to the database and JsonString maps to the REST service facade.
Here are the files:
package de.project.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.Serializable;
import java.util.Objects;
#JsonDeserialize(using = JsonStringDeserializer.class)
public class JsonString implements Serializable {
#JsonRawValue
#JsonValue
private String json;
public JsonString() { }
public JsonString(String json){
this.json= json;
}
public JsonString(Object value){
this.json= MapperSingleton.INSTANCE.writeValue(value);
}
protected enum MapperSingleton {
INSTANCE;
private final ObjectMapper objectMapper;
private MapperSingleton(){
this.objectMapper= new ObjectMapper();
this.objectMapper.registerModule(new JavaTimeModule());
this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
public String writeValue(Object value){
try {
return this.objectMapper.writeValueAsString(value);
} catch (JsonProcessingException ex) {
throw new WriteException("Write Java object value to json string failed!", ex);
}
}
public <T> T readValue(String jsonString, Class<T> valueType){
try {
return this.objectMapper.readValue(jsonString, valueType);
} catch (JsonProcessingException ex) {
throw new ReadException("Java object value from json string not found!", ex);
}
}
}
public String get(){
return this.json;
}
public <T> T read(Class<T> valueType){
return MapperSingleton.INSTANCE.readValue(this.json, valueType);
}
public void write(Object valueType){
this.json= MapperSingleton.INSTANCE.writeValue(valueType);
}
#Override
public boolean equals(Object obj){
if(obj instanceof JsonString){
var json2= (JsonString)obj;
return Objects.equals(this.json, json2.json);
}
return false;
}
#Override
public int hashCode() {
if(this.json == null){
return 42;
}
return this.json.hashCode();
}
public static class WriteException extends RuntimeException {
public WriteException(String message, Throwable throwable){
super(message, throwable);
}
}
public static class ReadException extends RuntimeException {
public ReadException(String message, Throwable throwable){
super(message, throwable);
}
}
}
package de.project.config;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
public class JsonStringDeserializer extends StdDeserializer<JsonString> {
public JsonStringDeserializer() {
this(null);
}
public JsonStringDeserializer(Class<?> vc) {
super(vc);
}
#Override
public JsonString deserialize(JsonParser jsonparser, DeserializationContext context)
throws IOException {
String jsonData= jsonparser.getCodec().readTree(jsonparser).toString();
return new JsonString(jsonData);
}
}
package de.project.config;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
import org.postgresql.util.PGobject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JsonStringDbMapper implements UserType
{
private static final Logger LOG = LoggerFactory.getLogger(JsonStringDbMapper.class);
/**
* Return the SQL type codes for the columns mapped by this type. The
* codes are defined on <tt>java.sql.Types</tt>.
* #see java.sql.Types
* #return int[] the typecodes
*/
#Override
public int[] sqlTypes() {
return new int[]{Types.OTHER}; // We use only one column for our type JsonString
}
/**
* The class returned by <tt>nullSafeGet()</tt>.
*
* #return Class
*/
#Override
public Class returnedClass() {
return JsonString.class;
}
/**
* Compare two instances of the class mapped by this type for persistence "equality".
* Equality of the persistent state.
*
* #param x
* #param y
* #return boolean
*/
#Override
public boolean equals(Object o, Object o1) throws HibernateException {
return Objects.equals(o, o1);
}
/**
* Get a hashcode for the instance, consistent with persistence "equality"
*/
#Override
public int hashCode(Object o) throws HibernateException {
return Objects.hashCode(o);
}
/**
* Retrieve an instance of the mapped class from a JDBC resultset. Implementors
* should handle possibility of null values.
*
*
* #param rs a JDBC result set
* #param names the column names
* #param session
*#param owner the containing entity #return Object
* #throws HibernateException
* #throws SQLException
*/
#Override
public Object nullSafeGet(ResultSet rs, String[] names,
SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
final var dbValue0 = rs.getObject(names[0]);
if(dbValue0 == null || rs.wasNull()){
return null;
}
if(dbValue0 instanceof PGobject){
var pgObj= (PGobject)dbValue0;
if("jsonb".equals(pgObj.getType())){
return new JsonString(pgObj.getValue());
}
throw new IllegalArgumentException("Unable to convert pgObj.type " + pgObj.getType()
+ " into JsonString");
}else{
throw new ClassCastException("Failed to convert " + dbValue0.getClass().getName()
+ " PGobject");
}
}
/**
* Write an instance of the mapped class to a prepared statement. Implementors
* should handle possibility of null values. A multi-column type should be written
* to parameters starting from <tt>index</tt>.
*
*
* #param st a JDBC prepared statement
* #param value the object to write
* #param index statement parameter index
* #param session
* #throws HibernateException
* #throws SQLException
*/
#Override
public void nullSafeSet(PreparedStatement st, Object value, int index,
SharedSessionContractImplementor session)
throws HibernateException, SQLException {
if(Objects.isNull(value)){
st.setNull(index, Types.OTHER);
}else{
var pgObj = new PGobject();
pgObj.setType("jsonb");
try {
var jsonString= (JsonString)value;
pgObj.setValue(jsonString.get());
st.setObject(index, pgObj);
} catch (Exception ex) {
LOG.error("value='{}'", value);
throw new IllegalArgumentException("Unable to convert JsonString into PGobject", ex);
}
}
}
/**
* Return a deep copy of the persistent state, stopping at entities and at
* collections. It is not necessary to copy immutable objects, or null
* values, in which case it is safe to simply return the argument.
*
* #param value the object to be cloned, which may be null
* #return Object a copy
*/
#Override
public Object deepCopy(Object o) throws HibernateException {
return o;
}
/**
* Are objects of this type mutable?
*
* #return boolean
*/
#Override
public boolean isMutable() {
return false;
}
/**
* Transform the object into its cacheable representation. At the very least this
* method should perform a deep copy if the type is mutable. That may not be enough
* for some implementations, however; for example, associations must be cached as
* identifier values. (optional operation)
*
* #param value the object to be cached
* #return a cacheable representation of the object
* #throws HibernateException
*/
#Override
public Serializable disassemble(Object value) throws HibernateException {
return new JsonString(((JsonString)value).get());
}
/**
* Reconstruct an object from the cacheable representation. At the very least this
* method should perform a deep copy if the type is mutable. (optional operation)
*
* #param cached the object to be cached
* #param owner the owner of the cached object
* #return a reconstructed object from the cacheable representation
* #throws HibernateException
*/
#Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return new JsonString(((JsonString)cached).get());
}
/**
* During merge, replace the existing (target) value in the entity we are merging to
* with a new (original) value from the detached entity we are merging. For immutable
* objects, or null values, it is safe to simply return the first parameter. For
* mutable objects, it is safe to return a copy of the first parameter. For objects
* with component values, it might make sense to recursively replace component values.
*
* #param original the value from the detached entity being merged
* #param target the value in the managed entity
* #return the value to be merged
*/
#Override
public Object replace(Object original, Object target, Object o2) throws HibernateException {
return original;
}
}
package de.project.config;
import java.sql.Types;
import org.hibernate.dialect.H2Dialect;
public class JsonbH2Dialect extends H2Dialect {
public JsonbH2Dialect(){
super();
// Das Datenbanksystem H2 kennt den passenden Datentyp OTHER für Java-Objekte.
// Im HibernateDialect H2Dialect fehlt das Mapping, welches wir hier ergänzen.
// Aktuell verwenden wir Types.OTHER für PGObject (jsonb von PostgreSQL).
this.registerColumnType(Types.OTHER, "OTHER");
}
}

Related

AEM Querybuilder Getting If Referenced

I help oversee (quality assurance) a website on AEM that has around 65,000 pages, 320,000 assets, and about 300 users able post and make changes. One thing that has been extremely helpful is a script a former IT employee wrote for us that used the querybuilder servlet to make queries and pull full lists of pages and assets. I've been able to take this output and build all sorts of automated reports using it.
The one thing I have not been able to figure out is how to find out if an asset or page is referenced by another page. The main thing I care about is just a simple true/false on if it is refereced or not. Ideally I would like it if it could be in the initial query and not have to do a query for each individual asset, but if that is the only way then I guess it in theory could be acceptable.
Just a sample query I could run currently to get some info on assets (I limited this one to 5 results for the sample):
http://localhost:4502/bin/querybuilder.json?p.hits=selective&p.offset=0&p.limit=5&p.properties=jcr%3acontent%2fmetadata%2fdc%3aformat%20jcr%3acontent%2fmetadata%2fdc%3atitle%20jcr%3apath%20&path=%2fcontent%2fdam&type=dam%3aAsset
Is there any way to add to that a field for if it is referenced? Or an array of all the references to it?
We are currently running AEM 6.2 but will soon be upgrading to 6.4.
Thank you!
There is an OOTB servlet that will return the list of pages that refer to a particular page or asset
To check if a page or asset is referenced, use
https://localhost:4502/bin/wcm/references?
_charset_=utf-8
&path=<path of the page>
&predicate=wcmcontent
&exact=false
The output will be a json response containing an array of references of the name 'pages'. If the page is not referenced it will be an empty array.
This servlet uses ReferenceSearch API the other answer mentions. If you need this value as JSON outside of AEM, you can straight away use the OOTB one without having to write your own servlet.
For your requirement, you can leverage the AssetReferenceSearch API which can give the details of Assets used in a page (node of type cq:Page).
You can use the following code to accomplish your task -
package org.redquark.aem.assets.core;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.commons.util.AssetReferenceSearch;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* #author Anirudh Sharma
*
*/
#Component(
service = Servlet.class,
property = {
"sling.servlet.methods=GET",
"sling.servlet.resourceTypes=cq/Page",
"sling.servlet.selectors=assetreferences",
"sling.servlet.extensions=json",
"service.ranking=1000"
}
)
public class FindReferencedAssetsServlet extends SlingSafeMethodsServlet {
// Generated serial version UID
private static final long serialVersionUID = 8446564170082865006L;
private final Logger log = LoggerFactory.getLogger(this.getClass());
private static final String DAM_ROOT = "/content/dam";
#Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
response.setContentType("application/json");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try {
// Get the current node reference from the resource object
Node currentNode = request.getResource().adaptTo(Node.class);
if (currentNode == null) {
// Every adaptTo() can return null, so let's handle the case here
// However, it is very unlikely
log.error("Cannot adapt resource {} to a node", request.getResource().getPath());
response.getOutputStream().print(new Gson().toString());
return;
}
// Using AssetReferenceSearch which will do all the work for us
AssetReferenceSearch assetReferenceSearch = new AssetReferenceSearch(currentNode, DAM_ROOT,
request.getResourceResolver());
Map<String, Asset> result = assetReferenceSearch.search();
List<AssetDetails> assetList = new LinkedList<>();
for (String key : result.keySet()) {
Asset asset = result.get(key);
AssetDetails assetDetails = new AssetDetails(asset.getName(), asset.getPath(), asset.getMimeType());
assetList.add(assetDetails);
}
String jsonOutput = gson.toJson(assetList);
response.getOutputStream().println(jsonOutput);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
The corresponding AssetDetails model class is as follows -
package org.redquark.aem.assets.core;
/**
* #author Anirudh Sharma
*/
public class AssetDetails {
private String name;
private String path;
private String mimeType;
/**
* #param name
* #param path
* #param mimeType
*/
public AssetDetails(String name, String path, String mimeType) {
this.name = name;
this.path = path;
this.mimeType = mimeType;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the path
*/
public String getPath() {
return path;
}
/**
* #param path the path to set
*/
public void setPath(String path) {
this.path = path;
}
/**
* #return the mimeType
*/
public String getMimeType() {
return mimeType;
}
/**
* #param mimeType the mimeType to set
*/
public void setMimeType(String mimeType) {
this.mimeType = mimeType;
}
}
Now, you can invoke this servlet by the following request -
http://localhost:4502/content/we-retail/language-masters/en/men.assetreferences.json.
This will give output in the following format
[
{
"name": "running-trail-man.jpg",
"path": "/content/dam/we-retail/en/activities/running/running-trail-man.jpg",
"mimeType": "image/jpeg"
},
{
"name": "enduro-trail-jump.jpg",
"path": "/content/dam/we-retail/en/activities/biking/enduro-trail-jump.jpg",
"mimeType": "image/jpeg"
},
{
"name": "indoor-practicing.jpg",
"path": "/content/dam/we-retail/en/activities/climbing/indoor-practicing.jpg",
"mimeType": "image/jpeg"
}
]
You can edit the AssetDetails class as per your requirement.
I hope this helps. Happy Coding!!!

how to generate html report if my Junit is not run by Ant but by JunitCore.run

I worked on a project in which testclasses are run via JunitCore.run(testClasses) not via Ant because I have to run the project even with no ANT framework (so no Testng for the same reason). But I still need to create html and xml reports same as JUNIT/ANT. How to generate them in my case?
Right now I found https://github.com/barrypitman/JUnitXmlFormatter/blob/master/src/main/java/barrypitman/junitXmlFormatter/AntXmlRunListener.java may be used to generate xml report. How do I generate html similar to junit-noframes.html? Are there existing methods to convert the TESTS-TestSuites.xml to junit-noframes.html and how? if not, how to generate the html? I do not even find the standard of the html format.
1) Write a test class
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MyTest{
#Test
public void test(){
int i=5;
int j=5;
assertTrue(i==j);
}
#Test
public void test2(){
int i=5;
int j=15;
assertTrue(i!=j);
}
}
2)Create a class which extends RunListner:
import org.junit.runner.Description;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
public class MyRunListner extends RunListener{
private int numberOfTestClass;
private int testExecuted;
private int testFailed;
private long begin;
public MyRunListner(int numberOfTestClass){
this.setBegin(System.currentTimeMillis());
this.numberOfTestClass = numberOfTestClass;
}
public void testStarted(Description description) throws Exception{
this.testExecuted += 1;
}
public void testFailure(Failure failure) throws Exception{
this.testFailed += 1;
}
/**
* #return the numberOfTestClass
*/
public int getNumberOfTestClass(){
return numberOfTestClass;
}
/**
* #param numberOfTestClass the numberOfTestClass to set
*/
public void setNumberOfTestClass(int numberOfTestClass){
this.numberOfTestClass = numberOfTestClass;
}
/**
* #return the testExecuted
*/
public int getTestExecuted(){
return testExecuted;
}
/**
* #param testExecuted the testExecuted to set
*/
public void setTestExecuted(int testExecuted){
this.testExecuted = testExecuted;
}
/**
* #return the testFailed
*/
public int getTestFailed(){
return testFailed;
}
/**
* #param testFailed the testFailed to set
*/
public void setTestFailed(int testFailed){
this.testFailed = testFailed;
}
/**
* #return the begin
*/
public long getBegin(){
return begin;
}
/**
* #param begin the begin to set
*/
public void setBegin(long begin){
this.begin = begin;
}
}
3) Generate the report.
import java.io.FileWriter;
import java.io.IOException;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
public class JUnitTest{
public static void main(String[] args){
JUnitTest junit = new JUnitTest();
junit.runTest();
}
public void runTest(){
try {
String filePath = "C:/temp";
String reportFileName = "myReport.htm";
Class[] myTestToRunTab = {MyTest.class};
int size = myTestToRunTab.length;
JUnitCore jUnitCore = new JUnitCore();
jUnitCore.addListener(new MyRunListner(myTestToRunTab.length));
Result result = jUnitCore.run(myTestToRunTab);
StringBuffer myContent = getResultContent(result,size);
writeReportFile(filePath+"/"+reportFileName,myContent);
}
catch (Exception e) {
}
}
private StringBuffer getResultContent(Result result,int numberOfTestFiles){
int numberOfTest = result.getRunCount();
int numberOfTestFail = result.getFailureCount();
int numberOfTestIgnore = result.getIgnoreCount();
int numberOfTestSuccess = numberOfTest-numberOfTestFail-numberOfTestIgnore;
int successPercent = (numberOfTest!=0) ? numberOfTestSuccess*100/numberOfTest : 0;
double time = result.getRunTime();
StringBuffer myContent = new StringBuffer("<h1>Junit Report</h1><h2>Result</h2><table border=\"1\"><tr><th>Test Files</th><th>Tests</th><th>Success</th>");
if ((numberOfTestFail>0)||(numberOfTestIgnore>0)) {
myContent.append("<th>Failure</th><th>Ignore</th>");
}
myContent.append("<th>Test Time (seconds)</th></tr><tr");
if ((numberOfTestFail>0)||(numberOfTestIgnore>0)) {
myContent.append(" style=\"color:red\" ");
}
myContent.append("><td>");
myContent.append(numberOfTestFiles);
myContent.append("</td><td>");
myContent.append(numberOfTest);
myContent.append("</td><td>");
myContent.append(successPercent);
myContent.append("%</td><td>");
if ((numberOfTestFail>0)||(numberOfTestIgnore>0)) {
myContent.append(numberOfTestFail);
myContent.append("</td><td>");
myContent.append(numberOfTestIgnore);
myContent.append("</td><td>");
}
myContent.append(Double.valueOf(time/1000.0D));
myContent.append("</td></tr></table>");
return myContent;
}
private void writeReportFile(String fileName,StringBuffer reportContent){
FileWriter myFileWriter = null;
try {
myFileWriter = new FileWriter(fileName);
myFileWriter.write(reportContent.toString());
}
catch (IOException e) {
}
finally {
if (myFileWriter!=null) {
try {
myFileWriter.close();
}
catch (IOException e) {
}
}
}
}
}
4) Finally our report is ready
I hope it helps you!
In fact I solved the problem myself in this way:
First I use https://code.google.com/p/reporting-junit-runner/source/browse/trunk/src/junitrunner/XmlWritingListener.java?spec=svn2&r=2
to create TESTS-*.xml
Then I write the following code myself to create TEST-SUITE.xml and junit-noframes.html. The idea is make use of API of ANT to create reports without really running test. so far the solution works for me.
Project project = new Project();
//a fake project feeding to ANT API so that latter not complain
project.setName("dummy");
project.init();
FileSet fs = new FileSet();
fs.setDir(new File(reportToDir));
fs.createInclude().setName("TEST-*.xml");
XMLResultAggregator aggregator = new XMLResultAggregator();
aggregator.setProject(project);
aggregator.addFileSet(fs);
aggregator.setTodir(new File(reportToDir));
//create TESTS-TestSuites.xml
AggregateTransformer transformer = aggregator.createReport();
transformer.setTodir(new File(reportToDir));
Format format = new Format();
format.setValue(AggregateTransformer.NOFRAMES);
transformer.setFormat(format);
//create junit-noframe.html
aggregator.execute();

NullPointerException when tyring to create table for mapped-superclass

I traced it down to the getDatastoreClass returning a null datastore class to the createPerImplementationColumnsForReferenceField.
I have tried both the 3.1.1 and now using 3.2.0-m4 release hoping that would fix my problem.
RDBMSStoreManager#getDatastoreClass(String className, ClassLoaderResolver clr);
It is returning a null datastore class to the
ReferenceMapping#createPerImplementationColumnsForReferenceField(boolean pk, boolean nullable, boolean serialised, boolean embedded, int fieldRole, ColumnMetaData[] columnMetaData, ClassLoaderResolver clr)
I am using the orm to annotate a mapped-superclass and this mapped-superclass does not have a table definition and both of my mapped-superclass throwing this exception.
499170 [http-bio-8080-exec-8] DEBUG DataNucleus.Datastore.Schema - Field [com.hp.vf.server.domain.AlertDefinition.isPublic] -> Column(s) ["ALERTDEFINITION"."ISPUBLIC"] using mapping of type "org.datanucleus.store.rdbms.mapping.java.BooleanMapping" (org.datanucleus.store.rdbms.mapping.datastore.SmallIntRDBMSMapping)
551964 [http-bio-8080-exec-8] DEBUG DataNucleus.Persistence - Managing Persistence of Class : com.hp.vf.analytics.shared.metric.Metric [Table : (none), InheritanceStrategy : subclass-table]
561964 [http-bio-8080-exec-8] ERROR DataNucleus.Datastore.Schema - An exception was thrown while adding/validating class(es) : null
java.lang.NullPointerException
at org.datanucleus.store.rdbms.mapping.java.ReferenceMapping.createPerImplementationColumnsForReferenceField(ReferenceMapping.java:452)
at org.datanucleus.store.rdbms.mapping.java.ReferenceMapping.prepareDatastoreMapping(ReferenceMapping.java:214)
at org.datanucleus.store.rdbms.mapping.java.ReferenceMapping.initialize(ReferenceMapping.java:110)
at org.datanucleus.store.rdbms.mapping.java.InterfaceMapping.initialize(InterfaceMapping.java:54)
In the Reference mapping, dc is null when trying to execute getIdMapping(), I have verified this in the debugger.
try
{
DatastoreClass dc = storeMgr.getDatastoreClass(implClass.getName(), clr);
m = dc.getIdMapping(); // DC is null
}
catch (NoTableManagedException ex)
{
// TODO Localise this message
throw new NucleusUserException("Cannot define columns for " + mmd.getFullFieldName() +
" due to " + ex.getMessage(), ex);
}
Here is the problematic class file.
public abstract class Metric implements IMetric {
/**
* Serialization ID
*/
private static final long serialVersionUID = 3806479436166940035L;
private Long id;
/**
* The name of the metric, this is not mandatory we have some metrics that
* may come back without names.
*/
protected String name;
/**
* This is an optional metric value that can be set by the script in order
* to add context to the execution of the metric.
*/
protected String context;
/**
* The list of violations associated with this metric.
*/
protected List<Violation> violations = null;
public Metric() {
violations = new ArrayList<Violation>();
}
/**
* Constructor that takes the name of the object and the value that it
* represents.
*
* #param name
* #param value
*/
public Metric(String name) {
this();
this.name = name;
}
/* (non-Javadoc)
* #see com.hp.vf.taskengine.shared.metric.IMetric#getName()
*/
#Override
public String getName() {
return name;
}
/* (non-Javadoc)
* #see com.hp.vf.taskengine.shared.metric.IMetric#setName(java.lang.String)
*/
#Override
public void setName(String name) {
this.name = name;
}
/**
* This is the context that represents the metric. This could have come from
* R and would be a Key:Value; pair of values used to calculate the value.
* For example if a metric was calculated for a product in houston for ISS
* the context may look like "ProdNum:1234;Factory:Houston;BUnit:ISS". This
* context is useful when chaining together tasks.
*
* #return String context used when chaining tasks together.
*/
public String getContext() {
return context;
}
/* (non-Javadoc)
* #see com.hp.vf.taskengine.shared.metric.IMetric#toString()
*/
public String toString() {
String debugString = "";
debugString += "Metric: " + name;
return debugString;
}
/* (non-Javadoc)
* #see com.hp.vf.taskengine.shared.metric.IMetric#hasMetricViolations()
*/
#Override
public boolean hasMetricViolations() {
return (violations != null && violations.size() > 0) ? true : false;
}
/* (non-Javadoc)
* #see com.hp.vf.taskengine.shared.metric.IMetric#getViolations()
*/
#Override
public List<IViolation> getViolations() {
return violations;
}
/* (non-Javadoc)
* #see com.hp.vf.taskengine.shared.metric.IMetric#setViolations(java.util.List)
*/
#Override
public void setViolations(List<IViolation> violations) {
this.violations = violations;
}
#Override
public Long getId() {
return id;
}
}
Here is an excert from my orm.xml file
<access>FIELD</access>
<mapped-superclass class="com.hp.vf.analytics.shared.metric.Metric" access="FIELD">
<attributes>
<basic name="name" />
<basic name="context" />
<one-to-many name="violations">
<cascade>
<cascade-all />
</cascade>
</one-to-many>
</attributes>
</mapped-superclass>
Am I doing something wrong or is this a bug?
I was able to resolve the problem using the tips from Datanucleus. I had multiple problems
Problems
The most obvious and silly on my part
If you have an one-to-many or one-to-one that uses an interface you must specify the target-entity.
/**
* The list of violations associated with this metric.
*/
protected List<IViolation> violations = null;
...
<one-to-many name="violations"
target-entity="com.vf.analytics.shared.metric.Violation">
<cascade>
<cascade-all />
</cascade>
</one-to-many>
Don't use generics, the jpa spec does not suppport them
Not sure if datanucleus supports generics but it but probably not recommended.
public MetricNumber< T extends Number> extends Metric implements IMetric {
T value;
}
If you extend an entity that is not annotated make sure that you add it to your orm.xml
I wasn't testing some of the objects so I assumed that datanucleus would ignore them but this appears not to be the case. I had extended third party classes that were not annotated but had not yet added them to the orm.xml, this was also causing null the null pointer exceptions I was getting.

Cannot serialize an Enum to GWT

I am unable to serialize an Enum to GWT if it implements java.io.Serializable. It will GWT compile successfully, but at runtime, I get the dreaded:
Type 'com....security..AdminPrivilege' was not assignable to 'com.google.gwt.user.client.rpc.IsSerializable' and did not have a custom field serializer.For security purposes, this type will not be serialized.: instance = Login to Console
If I implement com.google.gwt.user.client.rpc.IsSerializable it compiles and runs fine. I am trying to avoid IsSerializable, since this Enum is persisted in our DB and is referenced in non-GWT servlets. I do not want to introduce a GWT dependancy, even for that single class.
I've read most of the discussions on this topic here. I have:
added a serialVersionUid (which should not be necessary)
added a no-arg constructor (but this is an Enum, so it must be private - I suspect this may be the problem)
added a callable RPC method that returns the Enum and takes a Set of the Enum as an input argument (trying to get this Enum into the whitelist) -
For all other Enums, I generated a GWT version which implements IsSerializable. But this new Enum is too complex to generate and I need the methods from the Enum in the GWT code.
Thanks for any help on this.
My Enum is below. Notice it has an embedded Enum:
public enum AdminPrivilege implements java.io.Serializable {
// Privileges
MANAGE_XX("Manage XX", PrivilegeCategory.XX),
IMPORT_LICENSE("Import a License", PrivilegeCategory.XX),
SUBMIT_BUG("Submit a Bug", PrivilegeCategory.XX),
TEST_AD("Test AD", PrivilegeCategory.XX),
// Administrator Privileges
LOGIN("Login to XX", PrivilegeCategory.ADMIN),
MANAGE_ADMIN("Manage Administrators", PrivilegeCategory.ADMIN),
MANAGE_ROLE("Manage Roles", PrivilegeCategory.ADMIN),
// Task Privileges
CANCEL_TASK("Cancel Tasks", PrivilegeCategory.TASK), ;
private static final long serialVersionUID = 1L;
/**
* Defines the privilege categories.
*
*/
public enum PrivilegeCategory implements java.io.Serializable {
XX("XX"),
ADMIN("Administrator"),
TASK("Task"), ;
private static final long serialVersionUID = 2L;
private String displayValue;
// This constructor is required for GWT serialization
private PrivilegeCategory() {
}
private PrivilegeCategory(String displayValue) {
this.displayValue = displayValue;
}
#Override
public String toString() {
return displayValue;
}
}
private String displayValue;
private AdminPrivilege parentPrivilege;
private PrivilegeCategory privilegeCategory;
// This constructor is required for GWT serialization
private AdminPrivilege() {
}
private AdminPrivilege(String displayValue, PrivilegeCategory category) {
this.displayValue = displayValue;
this.privilegeCategory = category;
}
private AdminPrivilege(String displayValue, PrivilegeCategory category, AdminPrivilege parent) {
this(displayValue, category);
this.parentPrivilege = parent;
}
/**
* Return the parent privilege for this privilege.
*
* #return
*/
public AdminPrivilege getParentPrivilege() {
return parentPrivilege;
}
/**
* Return the category for this privilege.
*
* #return
*/
public PrivilegeCategory getPrivilegeCategory() {
return privilegeCategory;
}
/**
* Return the set of categories.
*
* #return
*/
public static Set<PrivilegeCategory> getPrivilegeCategories() {
Set<PrivilegeCategory> category = new HashSet<PrivilegeCategory>();
for (PrivilegeCategory c : PrivilegeCategory.values()) {
category.add(c);
}
return category;
}
/**
* Return the set of privileges for a category.
*
* #return
*/
public static Set<AdminPrivilege> getPrivileges(PrivilegeCategory category) {
Set<AdminPrivilege> privileges = new HashSet<AdminPrivilege>();
for (AdminPrivilege p : AdminPrivilege.values()) {
if (category.equals(p.getPrivilegeCategory())) {
privileges.add(p);
}
}
return privileges;
}
/**
* Return the set of child privileges for a specific privilege
*
* #param parent
* #return
*/
public static Set<AdminPrivilege> getChildPrivileges(AdminPrivilege parent) {
Set<AdminPrivilege> children = new HashSet<AdminPrivilege>();
for (AdminPrivilege priv : values()) {
if (parent.equals(priv.getParentPrivilege())) {
children.add(priv);
}
}
return children;
}
/**
* Return the set of privileges that are parent privileges
*
* #return
*/
public static Set<AdminPrivilege> getParentPrivileges() {
Set<AdminPrivilege> parents = new HashSet<AdminPrivilege>();
for (AdminPrivilege priv : values()) {
if (priv.getParentPrivilege() == null) {
parents.add(priv);
}
}
return parents;
}
}
}
Have you specified a parameterized constructor in your enum? If you have, and it has parameters, you need to remember to add a no-parameters constructor as well, even if you don't use it, because GWT will need it. Adding a parameterized constructor and forgetting to add a parameterless one gets me every time, at least with non-enum classes.

Vala: D-BUS object implementing interface, error with properties

Is it possible to have a class annotated with [DBus (name = ...)] implement an interface?
Following the example at https://live.gnome.org/Vala/DBusServerSample, I am implementing a D-BUS client/server application.
One thing that I found peculiar about the example was that there was no separate interface definition. I would like to have the interface used by the client side in a separate file, and have the server class implement that interface. That way I can have the compiler tell me when I miss something.
This does not appear to work with properties though. The following definition is compatible with what I have:
/* interface.vala */
namespace org.test {
[DBus (name = "org.test.Items")]
public interface IItems : Object {
/**
* The object paths to the item instances.
*
* These objects are of type org.test.items.Item.
*/
public abstract ObjectPath[] items {
owned get;
}
/**
* The signal that is emitted when a new item is added.
*
* When this signal is emitted, the item will be available.
*
* #param id
* The object path to the item instance.
*/
public signal void item_added(ObjectPath id);
/**
* The signal that is emitted when an item is removed.
*
* When this signal is emitted, the item will be unavailable.
*
* #param id
* The object path to the item instance.
*/
public signal void item_removed(ObjectPath id);
/**
* Adds a new item.
*
* The URL will be parsed, and if it contains a valid item, it will be
* added.
*
* #param url
* The URL to the item. This should typically be the URL of the
* RSS feed.
* #return the ID of the item added, which can be used to query D-BUS
* for it
* #throws IOError if a D-BUS error occurs
*/
public abstract ObjectPath add_item(string url) throws IOError;
/**
* Removes an item.
*
* #param id
* The ID of the item to remove.
* #throws IOError if a D-BUS error occurs
*/
public abstract void remove_item(ObjectPath id) throws IOError;
}
}
/* server.vala */
using Gee;
namespace org.test {
[DBus (name = "org.test.Items")]
public class Items : DBUSObject, IItems {
private ArrayList<Item> _items;
[DBus (visible = false)]
protected override void dbus_register(DBusConnection conn,
ObjectPath path) throws IOError {
conn.register_object(path, this);
}
[DBus (visible = false)]
public Items() {
base("org.test.Items", "/org/test", "Items", true);
_items = new ArrayList<Item>();
}
[DBus (visible = false)]
~Items() {
unregister();
}
/**
* #see interface.vala::org.test.IItems.comics
*/
public ObjectPath[] items {
owned get {
ObjectPath[] result = {};
foreach (var item in _items) {
result += new ObjectPath(item.path);
}
return result;
}
}
/**
* #see interface.vala::org.test.IItems.add_comic
*/
public ObjectPath add_item(string url) throws IOError {
/* . . . */
}
/**
* #see interface.vala::org.test.IItems.remove_item
*/
public void remove_item(ObjectPath id) throws IOError {
/* . . . */
}
}
}
When I compile it, I get no error from valac, but when the generated C code is compiled, the linker complains: undefined reference to 'org_test_items_get_items'.
This function is referenced by _dbus_org_test_items_get_items, but it does not exist
It's obviously a bug. The right place to report bugs is http://bugzilla.gnome.org .