Neo4j: Special characters and time lag - spring

Goal: I am trying to make a Neo4j instance of the DBLP database on the basis of the publicly available DBLP XML file available here. I have modeled the database as a bipartite graph where the authors are in one set and the publications in the other set. To obtain all coauthors of John Doe one has to make the following Cypher query:
MATCH (a:Author)-[:WROTE]->(publication)<-[:WROTE]-(b:Author) WHERE a.name = "John Doe" RETURN DISTINCT b"
Problem 1: There seems to be a problem partly related to special characters, such as ë, æ, í, etc. When I, in my browser at the address http://localhost:7474/browser/, enter the query "MATCH (a:Author)-[:WROTE]->(p)<-[:WROTE]-(b:Author) WHERE a.name = "Jan Arne Telle" RETURN DISTINCT b", I should get 58 unique results (coauthors), but I get 79 results. For instance, coauthor Daniël Paulusma is split into three results: "Dani", "ë", "l Paulusma". But in fact, I also get coauthor David Keldsen as three results: "David Keldsen", "David", and "Keldsen". So the problem is not only related to special characters.
Problem 2: Results for the above mentioned query were returned in 90697 ms.
EDIT: After making several such queries results are returned in 2000 ms to 4000 ms.
Here is all the code:
Entry point: Application.java:
package std;
import java.io.File;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.core.GraphDatabase;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.apache.xerces.util.SecurityManager;
#SpringBootApplication
public class Application implements CommandLineRunner {
#Configuration
#EnableNeo4jRepositories(basePackages = "std")
static class ApplicationConfig extends Neo4jConfiguration {
public ApplicationConfig() {
setBasePackage("std");
}
#Bean
GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("dblp.db");
}
}
#Autowired
PublicationRepository publicationRepository;
#Autowired
GraphDatabase graphDatabase;
public void run(String... args) throws Exception {
Transaction tx = graphDatabase.beginTx();
try {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
SecurityManager mgr = new SecurityManager();
mgr.setEntityExpansionLimit(3100000);
parser.setProperty("http://apache.org/xml/properties/security-manager", mgr);
SaxHandler handler = new SaxHandler(publicationRepository, graphDatabase);
handler.setTransaction(tx);
parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", true);
InputStream xmlInput = new FileInputStream("/Users/username/Documents/dblp.xml");
parser.parse(xmlInput, handler);
tx.success();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} finally {
tx.close();
}
}
public static void main(String[] args) throws Exception {
FileUtils.deleteRecursively(new File("dblp.db"));
SpringApplication.run(Application.class, args);
}
}
Author.java:
package std;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.support.index.IndexType;
#NodeEntity
public class Author {
#GraphId
private Long id;
#Indexed(indexName = "names", unique = true, indexType = IndexType.FULLTEXT)
private String name;
public Author() {
}
public Author(String name) {
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Author other = (Author) obj;
if (this.id != null && this.name != null && other.id != null && other.name != null) {
if (this.id.equals(other.id) && this.name.equals(other.name))
return true;
} else {
return true;
}
return false;
}
#Override
public int hashCode() {
return 31 * (this.id == null ? 1 : this.id.hashCode()) + 31 * (this.name == null ? 1 : this.name.hashCode());
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Publication.java:
package std;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;
import org.springframework.data.neo4j.support.index.IndexType;
#NodeEntity
public class Publication implements Serializable {
private static final long serialVersionUID = -6393545300391560520L;
#GraphId
Long nodeId;
private String type = "";
private String key = "";
private String mdate = "";
private String publtype = "";
private String reviewid = "";
private String rating = "";
#RelatedTo(type = "WROTE", direction = Direction.INCOMING)
private Set<Author> authors = new HashSet<Author>();
private String editor = "";
#Indexed(indexType = IndexType.FULLTEXT, indexName = "titles")
private String title = "";
private String booktitle = "";
private String pages = "";
private String year = "";
private String address = "";
private String journal = "";
private String volume = "";
private String number = "";
private String month = "";
private String url = "";
private String ee = "";
private String cdrom = "";
private String cite = "";
private String publisher = "";
private String note = "";
private String crossref = "";
private String isbn = "";
private String series = "";
private String school = "";
private String chapter = "";
public Publication() {
}
public void addAuthor(Author author) {
authors.add(author);
}
public Set<Author> getAuthors() {
return authors;
}
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
#Override
public String toString() {
return "TYPE: " + type + "\n"
+ "KEY: " + key + "\n"
+ "MDATE: " + mdate + "\n";
}
public Long getNodeId() {
return nodeId;
}
public void setNodeId(Long nodeId) {
this.nodeId = nodeId;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getMdate() {
return mdate;
}
public void setMdate(String mdate) {
this.mdate = mdate;
}
public String getPubltype() {
return publtype;
}
public void setPubltype(String publtype) {
this.publtype = publtype;
}
public String getReviewid() {
return reviewid;
}
public void setReviewid(String reviewid) {
this.reviewid = reviewid;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getEditor() {
return editor;
}
public void setEditor(String editor) {
this.editor = editor;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBooktitle() {
return booktitle;
}
public void setBooktitle(String booktitle) {
this.booktitle = booktitle;
}
public String getPages() {
return pages;
}
public void setPages(String pages) {
this.pages = pages;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getJournal() {
return journal;
}
public void setJournal(String journal) {
this.journal = journal;
}
public String getVolume() {
return volume;
}
public void setVolume(String volume) {
this.volume = volume;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getEe() {
return ee;
}
public void setEe(String ee) {
this.ee = ee;
}
public String getCdrom() {
return cdrom;
}
public void setCdrom(String cdrom) {
this.cdrom = cdrom;
}
public String getCite() {
return cite;
}
public void setCite(String cite) {
this.cite = cite;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getCrossref() {
return crossref;
}
public void setCrossref(String crossref) {
this.crossref = crossref;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getSeries() {
return series;
}
public void setSeries(String series) {
this.series = series;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getChapter() {
return chapter;
}
public void setChapter(String chapter) {
this.chapter = chapter;
}
}
PublicationRepository.java:
package std;
import org.springframework.data.neo4j.repository.GraphRepository;
public interface PublicationRepository extends GraphRepository<Publication> {
Publication findByTitle(String title);
}
SaxHandler.java:
package std;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.neo4j.graphdb.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.GraphDatabase;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxHandler extends DefaultHandler {
private Stack<String> qNameStack = new Stack<String>();
private Stack<Publication> publicationStack = new Stack<Publication>();
private String publicationType = null;
private PublicationRepository publicationRepository = null;
private Publication publication = null;
private Author author = null;
private String currentElement = null;
private String value = null;
private boolean insideTitle = false;
private GraphDatabase graphDatabase;
private Transaction tx = null;
private static int counter = 0;
public List<Publication> getPublications() {
return publications;
}
#Autowired
public SaxHandler(PublicationRepository publicationRepository, GraphDatabase graphDatabase) {
this.publicationRepository = publicationRepository;
this.graphDatabase = graphDatabase;
}
public void setTransaction(Transaction tx) {
this.tx = tx;
}
public void startElement(String uri, String localName, String tagName, Attributes attributes) throws SAXException {
storeTagName(tagName);
createEmptyPublication();
testIfEnteringTitle(tagName);
testIfPublicationTag(tagName);
testOnAttributes(tagName, attributes);
}
public void endElement(String uri, String localName, String tagName) throws SAXException {
testIfLeavingTitle(tagName);
removeNameOfLastVisitedTag();
testIfFinishedCreatingPublication(tagName);
}
public void characters(char ch[], int start, int length) throws SAXException {
storeContentsInCurrentPublication(ch, start, length);
}
/**
* Store the contents of the current tag in the corresponding field
* of the current publication.
*
* #param ch
* #param start
* #param length
*/
private void storeContentsInCurrentPublication(char ch[], int start, int length) {
value = new String(ch,start,length).trim();
if (value.length() == 0)
return;
publication = publicationStack.peek();
currentElement = qNameStack.peek();
if ("author".equals(currentElement)) {
author = new Author();
author.setName(value);
publication.addAuthor(author);
} else if ("editor".equals(currentElement)) {
publication.setEditor(value);
} else if ("title".equals(currentElement)) {
String title = publication.getTitle() + value;
publication.setTitle(title);
} else if ("booktitle".equals(currentElement)) {
publication.setBooktitle(value);
} else if ("pages".equals(currentElement)) {
publication.setPages(value);
} else if ("year".equals(currentElement)) {
publication.setYear(value);
} else if ("address".equals(currentElement)) {
publication.setAddress(value);
} else if ("journal".equals(currentElement)) {
publication.setJournal(value);
} else if ("volume".equals(currentElement)) {
publication.setVolume(value);
} else if ("number".equals(currentElement)) {
publication.setNumber(value);
} else if ("month".equals(currentElement)) {
publication.setMonth(value);
} else if ("url".equals(currentElement)) {
publication.setUrl(value);
} else if ("ee".equals(currentElement)) {
publication.setEe(value);
} else if ("cdrom".equals(currentElement)) {
publication.setCdrom(value);
} else if ("cite".equals(currentElement)) {
publication.setCite(value);
} else if ("publisher".equals(currentElement)) {
publication.setPublisher(value);
} else if ("note".equals(currentElement)) {
publication.setNote(value);
} else if ("crossref".equals(currentElement)) {
publication.setCrossref(value);
} else if ("isbn".equals(currentElement)) {
publication.setIsbn(value);
} else if ("series".equals(currentElement)) {
publication.setSeries(value);
} else if ("school".equals(currentElement)) {
publication.setSchool(value);
} else if ("chapter".equals(currentElement)) {
publication.setChapter(value);
} else if ("i".equals(currentElement) && isInsideTitleOrBooktitle()) {
String title = publication.getTitle() + "<i>" + value + "</i>";
publication.setTitle(title);
} else if ("sup".equals(currentElement) && isInsideTitleOrBooktitle()) {
String title = publication.getTitle() + "<sup>" + value + "</sup>";
publication.setTitle(title);
} else if ("sub".equals(currentElement) && isInsideTitleOrBooktitle()) {
String title = publication.getTitle() + "<sub>" + value + "</sub>";
publication.setTitle(title);
} else if ("tt".equals(currentElement) && isInsideTitleOrBooktitle()) {
String title = publication.getTitle() + "<tt>" + value + "</tt>";
publication.setTitle(title);
} else if ("ref".equals(currentElement) && isInsideTitleOrBooktitle()) {
String title = publication.getTitle() + "<ref>" + value + "</ref>";
publication.setTitle(title);
}
}
/**
* Returns true if and only if the parser is inside
* either a title or booktitle tag.
*
* #return true if and only if the parser is inside
* either a title or booktitle tag.
*/
private boolean isInsideTitleOrBooktitle() {
return insideTitle;
}
/**
* Checks if the parser is finished with one whole
* publication. If so, the publication is stored in
* the database.
*
* #param tagName
*/
private void testIfFinishedCreatingPublication(String tagName) {
if (publicationType.equals(tagName)) {
publicationRepository.save(publicationStack.pop());
if (++counter % 1000 == 0) {
System.out.println("Counter = " + counter);
tx.success();
tx.close();
tx = graphDatabase.beginTx();
}
}
}
/**
* Removes the tag name of the last visited tag
* from the stack.
*/
private void removeNameOfLastVisitedTag() {
qNameStack.pop();
}
/**
* Store the tag name on the stack.
*
* #param tagName
*/
private void storeTagName(String tagName) {
qNameStack.push(tagName);
}
/**
* Create an empty publication to be filled with data.
*/
private void createEmptyPublication() {
publication = new Publication();
}
/**
* Checks if the parser is entering a title or booktitle tag. If so
* is the case, then a boolean flag is set.
*
* #param tagName the name of the current tag
*/
private void testIfLeavingTitle(String tagName) {
if ("title".equals(tagName) || "booktitle".equals(tagName))
insideTitle = false;
}
/**
* Checks if the parser is entering a title or booktitle tag. If so
* is the case, then a boolean flag is set.
*
* #param tagName the name of the current tag
*/
private void testIfEnteringTitle(String tagName) {
if ("title".equals(tagName) || "booktitle".equals(tagName))
insideTitle = true;
}
/**
* Checks if the current tag is one of:
* - article, inproceedings, proceedings, book, incollection, phdthesis, mastersthesis, www
* If the current tag is one of these, then the type of the current publication is set
* to the corresponding value.
*
* #param tagName the name of the current tag.
*/
private void testIfPublicationTag(String tagName) {
if ("article".equals(tagName)) {
publication.setType("article");
} else if ("inproceedings".equals(tagName)) {
publication.setType("inproceedings");
} else if ("proceedings".equals(tagName)) {
publication.setType("proceedings");
} else if ("book".equals(tagName)) {
publication.setType("book");
} else if ("incollection".equals(tagName)) {
publication.setType("incollection");
} else if ("phdthesis".equals(tagName)) {
publication.setType("phdthesis");
} else if ("mastersthesis".equals(tagName)) {
publication.setType("mastersthesis");
} else if ("www".equals(tagName)) {
publication.setType("www");
}
}
/**
* Checks if the tag has any attributes. If so, the existing attribute
* values are stored.
*
* A tag with attributes is one of:
* - article, inproceedings, proceedings, book, incollection, phdthesis, mastersthesis, www
*
* #param tagName the name of the current tag
* #param attributes the attributes of the current tag, if any
*/
private void testOnAttributes(String tagName, Attributes attributes) {
if (attributes.getLength() > 0) {
publicationType = tagName;
if (attributes.getValue("key") != null) {
publication.setKey(attributes.getValue("key"));
}
if (attributes.getValue("mdate") != null) {
publication.setMdate(attributes.getValue("mdate"));
}
if (attributes.getValue("publtype") != null) {
publication.setMdate(attributes.getValue("publtype"));
}
if (attributes.getValue("reviewid") != null) {
publication.setMdate(attributes.getValue("reviewid"));
}
if (attributes.getValue("rating") != null) {
publication.setMdate(attributes.getValue("rating"));
}
publicationStack.push(publication);
}
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dblp</groupId>
<artifactId>graphdbcreator</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>neo4j</id>
<name>Neo4j</name>
<url>http://m2.neo4j.org/</url>
</repository>
</repositories>
</project>

For problem 1 try to setup a manual index with an analyzer fitting your needs. See http://blog.armbruster-it.de/2014/10/deep-dive-on-fulltext-indexing-with-neo4j/ for details on how to use custom analyzers.
Another option would be to use stemming logic on application side and store the stemmed name in a secondary property.
A third option is adding "SIMILAR" relationships between author nodes referring to the very same person.
Regarding problem 2: make sure to have an index on the name property for authors:
CREATE INDEX ON :Author(name)
The difference on query times for subsequent calls is easily explained with caching, read more on http://neo4j.com/docs/stable/configuration-caches.html

It seems my SAX handler was flawed. For instance, given a tag <author>Daniël Paulusma</author>, the parser would make one call to the characters() method for "Dani", another call to characters() for "ë", and a third call to characters() for "l Paulusma". I found a simple solution to this problem here: SAX parsing and special characters.

Related

AutoProtoSchemaBuilder is not generating proto file

Updated my code as per #Ryan Emerson suggestion but still i don't see any auto-generation of Impl file and proto file
#AutoProtoSchemaBuilder(
includeClasses = { Book.class, Author.class },
schemaFileName = "library.proto",
schemaFilePath = "proto/")
interface DummyInitializer extends SerializationContextInitializer {
}
Author.class
public class Author {
private final String name;
private final String surname;
#ProtoFactory
public Author(String name, String surname) {
this.name = (String)Objects.requireNonNull(name);
this.surname = (String)Objects.requireNonNull(surname);
}
#ProtoField(
number = 1
)
public String getName() {
return this.name;
}
#ProtoField(
number = 2
)
public String getSurname() {
return this.surname;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
Author author = (Author)o;
return this.name.equals(author.name) && this.surname.equals(author.surname);
} else {
return false;
}
}
public int hashCode() {
return Objects.hash(new Object[]{this.name, this.surname});
}
}
Book.class
public class Book {
private final String title;
private final String description;
private final int publicationYear;
private final Set<Author> authors;
#ProtoFactory
public Book(String title, String description, int publicationYear, Set<Author> authors) {
this.title = (String)Objects.requireNonNull(title);
this.description = (String)Objects.requireNonNull(description);
this.publicationYear = publicationYear;
this.authors = (Set)Objects.requireNonNull(authors);
}
#ProtoField(
number = 1
)
public String getTitle() {
return this.title;
}
#ProtoField(
number = 2
)
public String getDescription() {
return this.description;
}
#ProtoField(
number = 3,
defaultValue = "-1"
)
public int getPublicationYear() {
return this.publicationYear;
}
#ProtoField(
number = 4
)
public Set<Author> getAuthors() {
return this.authors;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
Book book = (Book)o;
return this.publicationYear == book.publicationYear && this.title.equals(book.title) && this.description.equals(book.description) && this.authors.equals(book.authors);
} else {
return false;
}
}
public int hashCode() {
return Objects.hash(new Object[]{this.title, this.description, this.publicationYear, this.authors});
}
}
context-initialzer class with over-ride methods
import org.infinispan.protostream.SerializationContext;
import java.io.UncheckedIOException;
public class contextInitializer implements DummyInitializer {
#Override
public String getProtoFileName() {
return null;
}
#Override
public String getProtoFile() throws UncheckedIOException {
return null;
}
#Override
public void registerSchema(SerializationContext serCtx) {
}
#Override
public void registerMarshallers(SerializationContext serCtx) {
}
}
Then ClassA that instantiates context-initializer
public class classA {
DummyInitializer myInterface= new contextInitializer();
//Create a new cache instance
public void startCache() {
{
try {
manager = new DefaultCacheManager("src/main/resources/infinispan.xml");
GlobalConfigurationBuilder builder= new GlobalConfigurationBuilder();
builder.serialization().addContextInitializers(myInterface);
System.out.println("------------------>"+ builder.serialization().addContextInitializers(myInterface));
cache = manager.getCache();
System.out.println(cache.getName()+" is initialized ");
} catch (IOException e) {
throw new IllegalArgumentException("Failed to initialize cache due to IO error",e);
}
}
}
Maven
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-bom</artifactId>
<version>${infinispan.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-processor</artifactId>
<scope>provided</scope>
</dependency>
I am still not seeing any auto-generated proto file. Can someone tell me what am i doing wrong ?
You also need to add the org.infinispan.protostream:protostream-processor artifact as a dependency in order for code to be generated:
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-processor</artifactId>
<version>4.3.2.Final</version>
</dependency>
Once that's present, a DummyInitializerImpl.java class will be generated that automatically registers the proto file and marshallers for the Book and Author classes. Remember that these classes must also have protostream annotations in order for the schema and marshallers to be generated. Please see the documentation for code examples.
There are two issues with your current code:
You have provided a DummyInitializerImpl class, but that is what should be generated by #AutoProtoSchemaBuilder.
In your DummyInitializerImpl you're trying to register the Infinispan UUIDMarshaller for the Book and Author types. This won't work as that marshaller is designed for the java UUID class.
I suspect that the two issues are due to a missunderstanding of how the code generation works. If you just required a SerializationContextInitializer for the Author and Book classes, it's not necessary to create the DummyInitializerImpl manually and you definitely don't need to utilise the UUIDMarshaller.
You are not saying which build system is used. maven maybe ? Did you add the protostream annotation processor as a dependency? Having a definite answer to these questions would help unriddle the issue of code generation. And after, we still need to find out who is supposed to initialize that dummyInitializer field.

Spring AbstractRequestLoggingFilter fails with OOM on big requests

If I enable setIncludePayload(true) and I send a large request to servlet, application fails with OOM error.
I use Spring 3.2.8.
What can be wrong?
The problem is that this filter is not suitable for production. It caches everything in byte array buffer which give OOM with large requests like file uploads.
I altered source code so that this problem is avoided, see below.
Note: payload is only accessible in afterRequest method, because otherwice we would need to save request body to temporal file.
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
/**
* org.springframework.web.filter.AbstractRequestLoggingFilter will fail with OOM on large file upload. We fix it with limited size of byte buffer
*/
public abstract class AbstractRequestLoggingWithMaxSizeCheckFilter extends OncePerRequestFilter {
public static final String DEFAULT_BEFORE_MESSAGE_PREFIX = "Before request [";
public static final String DEFAULT_BEFORE_MESSAGE_SUFFIX = "]";
public static final String DEFAULT_AFTER_MESSAGE_PREFIX = "After request [";
public static final String DEFAULT_AFTER_MESSAGE_SUFFIX = "]";
private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 50;
private boolean includeQueryString = false;
private boolean includeClientInfo = false;
private boolean includePayload = false;
private int maxPayloadLength = 50;
private String beforeMessagePrefix = "Before request [";
private String beforeMessageSuffix = "]";
private String afterMessagePrefix = "After request [";
private String afterMessageSuffix = "]";
public AbstractRequestLoggingWithMaxSizeCheckFilter() {
}
public void setIncludeQueryString(boolean includeQueryString) {
this.includeQueryString = includeQueryString;
}
protected boolean isIncludeQueryString() {
return this.includeQueryString;
}
public void setIncludeClientInfo(boolean includeClientInfo) {
this.includeClientInfo = includeClientInfo;
}
protected boolean isIncludeClientInfo() {
return this.includeClientInfo;
}
public void setIncludePayload(boolean includePayload) {
this.includePayload = includePayload;
}
protected boolean isIncludePayload() {
return this.includePayload;
}
public void setMaxPayloadLength(int maxPayloadLength) {
Assert.isTrue(maxPayloadLength >= 0, "'maxPayloadLength' should be larger than or equal to 0");
this.maxPayloadLength = maxPayloadLength;
}
protected int getMaxPayloadLength() {
return this.maxPayloadLength;
}
public void setBeforeMessagePrefix(String beforeMessagePrefix) {
this.beforeMessagePrefix = beforeMessagePrefix;
}
public void setBeforeMessageSuffix(String beforeMessageSuffix) {
this.beforeMessageSuffix = beforeMessageSuffix;
}
public void setAfterMessagePrefix(String afterMessagePrefix) {
this.afterMessagePrefix = afterMessagePrefix;
}
public void setAfterMessageSuffix(String afterMessageSuffix) {
this.afterMessageSuffix = afterMessageSuffix;
}
protected boolean shouldNotFilterAsyncDispatch() {
return false;
}
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
boolean isFirstRequest = !this.isAsyncDispatch((HttpServletRequest) request);
if (this.isIncludePayload() && isFirstRequest) {
request = new AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper((HttpServletRequest) request, maxPayloadLength);
}
if (isFirstRequest) {
this.beforeRequest((HttpServletRequest) request, this.getBeforeMessage((HttpServletRequest) request));
}
try {
filterChain.doFilter((ServletRequest) request, response);
} finally {
if (!this.isAsyncStarted((HttpServletRequest) request)) {
this.afterRequest((HttpServletRequest) request, this.getAfterMessage((HttpServletRequest) request));
}
}
}
private String getBeforeMessage(HttpServletRequest request) {
return this.createMessage(request, this.beforeMessagePrefix, this.beforeMessageSuffix);
}
private String getAfterMessage(HttpServletRequest request) {
return this.createMessage(request, this.afterMessagePrefix, this.afterMessageSuffix);
}
protected String createMessage(HttpServletRequest request, String prefix, String suffix) {
StringBuilder msg = new StringBuilder();
msg.append(prefix);
msg.append("uri=").append(request.getRequestURI());
if (this.isIncludeQueryString()) {
msg.append('?').append(request.getQueryString());
}
if (this.isIncludeClientInfo()) {
String client = request.getRemoteAddr();
if (StringUtils.hasLength(client)) {
msg.append(";client=").append(client);
}
HttpSession session = request.getSession(false);
if (session != null) {
msg.append(";session=").append(session.getId());
}
String user = request.getRemoteUser();
if (user != null) {
msg.append(";user=").append(user);
}
}
if (this.isIncludePayload() && request instanceof AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper) {
AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper wrapper = (AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper) request;
byte[] buf = wrapper.toByteArray();
if (buf.length > 0) {
String payload;
try {
payload = new String(buf, wrapper.getCharacterEncoding());
} catch (UnsupportedEncodingException var10) {
payload = "[unknown]";
}
msg.append(";payload=").append(payload);
}
}
msg.append(suffix);
return msg.toString();
}
protected abstract void beforeRequest(HttpServletRequest var1, String var2);
protected abstract void afterRequest(HttpServletRequest var1, String var2);
private static class RequestCachingRequestWrapper extends HttpServletRequestWrapper {
private final ByteArrayOutputStream bos;
private final ServletInputStream inputStream;
private BufferedReader reader;
private int maxPayloadLength;
private boolean capped;
private RequestCachingRequestWrapper(HttpServletRequest request, int maxPayloadLength) throws IOException {
super(request);
this.bos = new ByteArrayOutputStream();
this.inputStream = new AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper.RequestCachingInputStream(request.getInputStream());
this.maxPayloadLength = maxPayloadLength;
}
public ServletInputStream getInputStream() throws IOException {
return this.inputStream;
}
public String getCharacterEncoding() {
return super.getCharacterEncoding() != null ? super.getCharacterEncoding() : "ISO-8859-1";
}
public BufferedReader getReader() throws IOException {
if (this.reader == null) {
this.reader = new BufferedReader(new InputStreamReader(this.inputStream, this.getCharacterEncoding()));
}
return this.reader;
}
private byte[] toByteArray() {
return this.bos.toByteArray();
}
private class RequestCachingInputStream extends ServletInputStream {
private final ServletInputStream is;
private RequestCachingInputStream(ServletInputStream is) {
this.is = is;
}
public int read() throws IOException {
int ch = this.is.read();
if (ch != -1) {
if (!capped) {
AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper.this.bos.write(ch);
if (AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper.this.bos.size() >= maxPayloadLength) {
AbstractRequestLoggingWithMaxSizeCheckFilter.RequestCachingRequestWrapper.this.bos.write("...(truncated)".getBytes("UTF-8"));
capped = true;
}
}
}
return ch;
}
}
}
}

Retrieve a user's id and keep it as long as the session is active in jsf

I have a web application in jsf where I would retrieve the id of the user for reuse in other managedbeans.
Each user has a session that is created as soon as it is autehtification the data are stored in a Mysql DB. I would like to retrieve and keep the id of the user store as long as the session is active. Can you help me please?
this are my managedBean
#ManagedBean
#SessionScoped
public class ProfManagedBeans {
private int idp;
private String email, pwd;
public HttpSession hs;
public ProfManagedBeans() {
}
public int getIdp() {
return idp;
}
public void setIdp(int id) {
this.idp = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public static PreparedStatement pstmt=null;
public static ResultSet rs = null;
public String query="";
public int dbid;
public String dbemail, dbpwd;
public String getDbemail() {
return dbemail;
}
public String getDbpwd() {
return dbpwd;
}
public void dbData(String uEmail){
DbConnect.getConnection();
query = "SELECT * FROM professeur where email='" + uEmail +"'";
try {
pstmt = DbConnect.conn.prepareStatement(query);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
dbid = rs.getInt("id_pr");
dbemail = rs.getString("email");
dbpwd = rs.getString("pwd");
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Exception " + e);
}
}
public int getIdProf(){
dbData(email);
String ids = hs.getId();
if(ids == hs.getId()){
idp = dbid;
return idp;
}
return 0;
}
public String login(){
dbData(email);
if (email.equals(dbemail)==true&&pwd.equals(dbpwd)==true) {
hs = Util.getSession();
hs.setAttribute("email", dbemail);
System.out.println(hs.getId());
System.out.println(getIdProf());
return "success.xhtml";
}
else {
FacesMessage fm = new FacesMessage("Email error", "ERROR MSG");
fm.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, fm);
return "LoginProf.xhtml";
}
}
public String logout() {
hs.invalidate();
return "/LoginProf.xhtml";
}
}
public class QuestionManagedBeans {
private int id,id_pr;
private String nomDesc, question, type;
private String reponse;
private double noteV, noteF;
private String niveau, matiere,chapitre, explication;
public static PreparedStatement pstmt=null;
public static ResultSet rs = null;
public String query="";
private ArrayList<Niveau> list ;
public QuestionManagedBeans() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNomDesc() {
return nomDesc;
}
public void setNomDesc(String nomDesc) {
this.nomDesc = nomDesc;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getReponse() {
return reponse;
}
public void setReponse(String reponse) {
this.reponse = reponse;
}
public double getNoteV() {
return noteV;
}
public void setNoteV(double noteV) {
this.noteV = noteV;
}
public double getNoteF() {
return noteF;
}
public void setNoteF(double noteF) {
this.noteF = noteF;
}
public String getNiveau() {
return niveau;
}
public void setNiveau(String niveau) {
this.niveau = niveau;
}
public String getMatiere() {
return matiere;
}
public void setMatiere(String matiere) {
this.matiere = matiere;
}
public String getExplication() {
return explication;
}
public String getChapitre() {
return chapitre;
}
public void setChapitre(String chapitre) {
this.chapitre = chapitre;
}
public void setExplication(String explication) {
this.explication = explication;
}
public int getId_pr() {
return id_pr;
}
public void setId_pr() {
ProfManagedBeans p = new ProfManagedBeans();
this.id_pr = p.getIdProf();
}
public boolean insertQuestion() throws SQLException {
Question q = new Question(id,nomDesc, question, type,reponse,noteV, noteF,niveau, matiere, chapitre ,explication,id_pr);
setNomDesc("");
setQuestion("");
setType("");
setReponse("");
setNoteV(0.0);
setNoteF(0.0);
setNiveau("");
setMatiere("");
setChapitre("");
setExplication("");
return new InsertDao().insertQuestionTF(q);
}
}
I want to recover the id of the proffessor as soon as it is authenticated to be able to add it to the question table
In InsertDao.java
public boolean insertQuestionTF(Question qu) throws SQLException{
DbConnect.getConnection();
ProfManagedBeans p = new ProfManagedBeans();
query ="INSERT INTO `question_tf` (`nomdesc`, `question`, `type`, `reponse`, `noteV`, `noteF`, `niveau`, `matiere`, `chapitre`, `explication`, `id_pr`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
pstmt = DbConnect.conn.prepareStatement(query);
pstmt.setString(1, qu.getNomDesc());
pstmt.setString(2, qu.getQuestion());
pstmt.setString(3, qu.getType());
pstmt.setString(4, qu.getReponse());
pstmt.setDouble(5, qu.getNoteV());
pstmt.setDouble(6, qu.getNoteF());
pstmt.setString(7, qu.getNiveau());
pstmt.setString(8, qu.getMatiere());
pstmt.setString(9, qu.getChapitre());
pstmt.setString(10, qu.getExplication());
pstmt.setInt(11, p.getIdProf());
System.out.println("insert " + p.getIdProf());
if (pstmt == null) {
throw new SQLException();
} else {
return pstmt.executeUpdate() > 0;
}
}

AsyncTask Crashing on postExecute

I have a problem when using asyntask to query all the data in a table and put it in an array List and then send it to the server. I am able to send the data to the server successfully. But the application crashes on the postexecute of the asynctask giving the following error:
(W/System.err﹕ org.json.JSONException: Value ["com.atlantis.eclectics.agentbank.SyncMembersActivity$MemberRecords#41b06d18","com.atlantis.eclectics.agentbank.SyncMembersActivity$MemberRecords#41b070b8"] of type org.json.JSONArray cannot be converted to JSONObject)..
What could be problem? Where am i getting it wrong. Someone please help...Thanks very much.
package com.practical.tasks.school;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
#SuppressWarnings("deprecation")
public class SyncMembersActivity extends ActionBarActivity {
CustomHttpClient jParser = new CustomHttpClient();
ListView lstView;
public MainDB dbs;
public SQLiteDatabase db;
public static String fname;
String username = "atlantis";
String password = "#t1#ntis";
Button submit;
String statusN = "NO";
String statusY = "YES";
String url = "http://123.456.78.90:1234/Api/create/Post";
String FirstName = "";
String SecondName = "";
String MobileNumber = "";
String DateofBirth = "";
String Gender = "";
String GroupName = "";
String GroupAccountNo = "";
String IdentificationID = "";
String IdentificationType = null;
String CreatedBy = null;
String Residence = "";
private int notificationIdOne = 111;
private int numMessagesOne = 0;
private NotificationManager myNotificationManager;
String account_statusY = "True";
private ProgressDialog prgDialog;
private ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sync_members);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeActionContentDescription("Services");
getSupportActionBar().setDisplayUseLogoEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#5A92F7")));
lstView = (ListView) findViewById(R.id.lstSample);
submit = (Button) findViewById(R.id.upload);
prgDialog = new ProgressDialog(this);
prgDialog.setMessage("Please wait...");
prgDialog.setCancelable(false);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (isNetworkAvailable(getApplicationContext())) {
new HttpAsyncTask().execute(FirstName, SecondName, MobileNumber, DateofBirth, Gender, GroupName, GroupAccountNo, IdentificationID, IdentificationType, CreatedBy, Residence);
} else {
showAlert("No internet Connectivity...");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sync_members, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class MemberRecords {
String FirstName;
String SecondName;
String MobileNumber;
String DateofBirth;
String Gender;
String GroupName;
String GroupAccountNo;
String IdentificationID;
String IdentificationType;
String CreatedBy;
String Residence;
public String getFirstName() {
return FirstName;
}
public String getSecondName() {
return SecondName;
}
public String getMobileNumber() {
return MobileNumber;
}
public String getDateofBirth() {
return DateofBirth;
}
public String getGender() {
return Gender;
}
public String getGroupName() {
return GroupName;
}
public String getGroupAccountNo() {
return GroupAccountNo;
}
public String getIdentificationID() {
return IdentificationID;
}
public String getIdentificationType() {
return IdentificationType;
}
public String getCreatedBy() {
return CreatedBy;
}
public String getResidence() {
return Residence;
}
public void setFirstName(String newfirstName) {
FirstName = newfirstName;
}
public void setSecondName(String newSecondName) {
SecondName = newSecondName;
}
public void setMobileNumber(String mobileNumber) {
MobileNumber = mobileNumber;
}
public void setDateofBirth(String dateofBirth) {
DateofBirth = dateofBirth;
}
public void setGender(String gender) {
Gender = gender;
}
public void setGroupName(String groupName) {
GroupName = groupName;
}
public void setGroupAccountNo(String groupAccountNo) {
GroupAccountNo = groupAccountNo;
}
public void setIdentificationID(String identificationID) {
IdentificationID = identificationID;
}
public void setIdentificationType(String identificationType) {
IdentificationType = identificationType;
}
public void setCreatedBy(String createdBy) {
CreatedBy = createdBy;
}
public void setResidence(String residence) {
Residence = residence;
}
}
public boolean isConnected() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
public static String POST(String url, MemberRecords my) {
InputStream inputStream;
String result = "";
String username = "atlantis";
String password = "#t1#ntis";
Integer n;
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost("http://41.186.47.26:4433/Api/Account/PostAddSignatory");
String json = "";
Log.e("Url", "Url Here:" + url);
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("FirstName", my.getFirstName());
jsonObject.accumulate("SecondName", my.getSecondName());
jsonObject.accumulate("MobileNumber", my.getMobileNumber());
jsonObject.accumulate("DateofBirth", my.getDateofBirth());
jsonObject.accumulate("Gender", my.getGender());
jsonObject.accumulate("GroupName", my.getGroupName());
jsonObject.accumulate("GroupAccountNo", my.getGroupAccountNo());
jsonObject.accumulate("IdentificationID", my.getIdentificationID());
jsonObject.accumulate("IdentificationType", my.getIdentificationType());
jsonObject.accumulate("CreatedBy", my.getCreatedBy());
jsonObject.accumulate("Residence", my.getResidence());
json = jsonObject.toString();
Log.e("Url", "Request:" + json);
StringEntity se = new StringEntity(json);
httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
Header header = new BasicScheme().authenticate(credentials, httpPost);
httpPost.addHeader(header);
httpPost.setEntity(se);
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.e("InputStream", e.getLocalizedMessage());
//e.printStackTrace();
}
// 11. return result
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException, JSONException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
Log.e("Url", "Response:" + result);
inputStream.close();
return result;
}
private class HttpAsyncTask extends AsyncTask<String, Void,String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
prgDialog.show();
}
#Override
protected String doInBackground(String... urls) {
dbs = new MainDB(SyncMembersActivity.this);
db = dbs.getWritableDatabase();
Integer n=null;
MemberRecords my = new MemberRecords();
List<MemberRecords> member_list = new ArrayList<>();
try {
Cursor cursor = db.rawQuery("SELECT * FROM tbl_memberData" +
" where sync_status = '" + statusN + "' AND account_status = '" + account_statusY + "'", null);
if (cursor.moveToFirst()) {
do {
my = new MemberRecords();
my.setGroupName(cursor.getString(1));
my.setIdentificationID(cursor.getString(2));
my.setIdentificationType(cursor.getString(3));
my.setFirstName(cursor.getString(4));
my.setSecondName(cursor.getString(5));
my.setDateofBirth(cursor.getString(6));
my.setMobileNumber(cursor.getString(7));
my.setGender(cursor.getString(8));
my.setGroupAccountNo(cursor.getString(9));
my.setCreatedBy(cursor.getString(10));
my.setResidence(cursor.getString(11));
member_list.add(my);
} while (cursor.moveToNext());
}
cursor.close();
} catch (NumberFormatException e) {
e.printStackTrace();
}
for ( n = 0; n < member_list.size(); n++) {
POST(url, member_list.get(n));
}
db.close();
return String.valueOf(member_list);
}
//onPostExecute displays the results of the AsyncTask.
//Response format
//Response:{"ResponseCode":"00","ResponseMsg":"Successful"}
//Response:{"ResponseCode":"01","ResponseMsg":"Failed"}
#Override
protected void onPostExecute (String result){
prgDialog.dismiss();
Toast.makeText(getBaseContext(),"Saved Successfully",Toast.LENGTH_LONG).show();
dbs = new MainDB(SyncMembersActivity.this);
db = dbs.getWritableDatabase();
String updateQuery = "Update tbl_memberData set sync_status = '" + statusY + "' where account_status='" + account_statusY + "'";
db.execSQL(updateQuery);
String success="";
String message="";
try {
JSONObject jsonBreaker = new JSONObject(result);
success = jsonBreaker.getString("ResponseCode");
message = jsonBreaker.getString("ResponseMsg");
if (success.equalsIgnoreCase("00")) {
prgDialog.dismiss();
showAlert(message);
} else if (success.equalsIgnoreCase("01")) {
prgDialog.dismiss();
//do
showAlert(message);
} else {
prgDialog.dismiss();
Toast.makeText(getBaseContext(), "Error, Please try again...", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private void showAlert(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setTitle("Response from Server")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
AlertDialog alert = builder.create();
alert.show();
}
public boolean isNetworkAvailable(final Context context) {
final ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
}
If you would baste stacktrace and opoint where rxception is thrown would be much easier.
Anyway
Your result is probably and JSONArray not an JSONObject so this
JSONObject jsonBreaker = new JSONObject(result);
Is causing the exception. Construct JSONArray insteed of JSONObject and this shoult be fine (this is my blind guess cuz no viable info here)

Validation Error: value is not vaild

This error is happening sometimes. It's not always that happens, and so I am not able to find solution. The error happens when I select an item from the list (I repeat, the error only happens SOMETIMES).
I've implemented the methods 'equals' and 'hashCode' and created the converter.
See the code below:
<p:outputLabel for="pais" value="País:" />
<p:selectOneMenu id="pais" value="#{clienteController.pais}" >
<f:selectItem itemLabel="Selecione..." itemValue="" />
<f:selectItems value="#{clienteController.paises}" var="p" itemLabel="#{p.nome}" itemValue="#{p}"/>
<p:ajax event="change" listener="#{clienteController.actionCarregaUfs}" update="uf" />
</p:selectOneMenu>
package control;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import dao.PaisDao;
import dao.PaisDaoImp;
import model.Pais;
#FacesConverter(forClass=Pais.class)
public class PaisConverter implements Converter {
#Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
if (value != null && !value.equals("")) {
PaisDao paisDao = new PaisDaoImp();
return paisDao.findById(Integer.valueOf(value));
}
return null;
}
#Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
if (value instanceof Pais) {
Pais pais = (Pais) value;
return String.valueOf(pais.getCodigo());
}
return "";
}
}
package control;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import dao.ClienteDao;
import dao.ClienteDaoImp;
import dao.PaisDaoImp;
import dao.TipoDocumentoDaoImp;
import dao.UnidadeFederacaoDaoImp;
import model.Cliente;
import model.Municipio;
import model.Pais;
import model.TipoDocumento;
import model.UnidadeFederacao;
#ManagedBean
#ViewScoped
public class ClienteController implements Serializable {
private Cliente cliente;
private DataModel listaClientes;
private List<Cliente> filteredClientes;
private Boolean isInsercao;
private Boolean isAlteracao;
private Pais pais;
private List<Pais> paises;
private List<UnidadeFederacao> ufs;
private UnidadeFederacao uf;
private List<Municipio> municipios;
private List<TipoDocumento> tiposDocumentos;
public ClienteController() {
this.cliente = new Cliente();
this.pais = new Pais();
this.ufs = new ArrayList<UnidadeFederacao>();
this.uf = new UnidadeFederacao();
this.municipios = new ArrayList<Municipio>();
this.tiposDocumentos = new ArrayList<TipoDocumento>();
}
public List<Cliente> getFilteredClientes() {
return filteredClientes;
}
public void setFilteredClientes(List<Cliente> filteredClientes) {
this.filteredClientes = filteredClientes;
}
public DataModel getListarClientes() {
List<Cliente> lista = new ClienteDaoImp().list();
listaClientes = new ListDataModel(lista);
return listaClientes;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public String prepararAdicionarCliente() {
cliente = new Cliente();
isInsercao = true;
isAlteracao = false;
return "gerenciarCliente";
}
public String prepararAlterarCliente() {
isInsercao = false;
isAlteracao = true;
this.pais = this.cliente.getMunicipio().getUf().getPais();
this.uf = this.cliente.getMunicipio().getUf();
return "gerenciarCliente";
}
public String excluirCliente() {
Cliente clienteTemp = (Cliente) (listaClientes.getRowData());
ClienteDao dao = new ClienteDaoImp();
dao.remove(clienteTemp);
return "index";
}
public String actExcluirCliente() {
ClienteDao dao = new ClienteDaoImp();
dao.remove(cliente);
return "index";
}
public String adicionarCliente() {
System.out.println("Adicionando Cliente...");
ClienteDao dao = new ClienteDaoImp();
dao.save(cliente);
return "index";
}
public String alterarCliente() {
ClienteDao dao = new ClienteDaoImp();
dao.update(cliente);
return "index";
}
public Boolean getIsInsercao() {
return isInsercao;
}
public void setIsInsercao(Boolean insercao) {
this.isInsercao = insercao;
}
public Boolean getIsAlteracao() {
return isAlteracao;
}
public void setIsAlteracao(Boolean alteracao) {
this.isAlteracao = alteracao;
}
/**************************************************/
public Pais getPais() {
return pais;
}
public void setPais(Pais pais) {
this.pais = pais;
}
public List<Pais> getPaises() {
paises = new PaisDaoImp().list();
return paises;
}
public void setPaises(List<Pais> paises) {
this.paises = paises;
}
public List<UnidadeFederacao> getUfs() {
return ufs;
}
public void setUfs(List<UnidadeFederacao> ufs) {
this.ufs = ufs;
}
public UnidadeFederacao getUf() {
return uf;
}
public void setUf(UnidadeFederacao uf) {
this.uf = uf;
}
public List<Municipio> getMunicipios() {
return municipios;
}
public void setMunicipios(List<Municipio> municipios) {
this.municipios = municipios;
}
public List<UnidadeFederacao> getUfsByPais() {
ufs = new PaisDaoImp().getUFsByPais(this.pais.getCodigo());
return ufs;
}
public void actionCarregaUfs() {
ufs.clear();
if (this.pais != null) {
if (!String.valueOf(this.pais.getCodigo()).equals("")) {
this.getUfsByPais();
}
}
}
public List<Municipio> getMunicipiossByUf() {
municipios = new UnidadeFederacaoDaoImp().getMunicipiosByUF(this.uf.getCodigo());
return municipios;
}
public void actionCarregaMunicipios() {
municipios.clear();
try {
this.uf.getCodigo();
} catch(NullPointerException e) {
return;
}
this.getMunicipiossByUf();
}
public List<TipoDocumento> getTiposDocumentos() {
tiposDocumentos = new TipoDocumentoDaoImp().list();
return tiposDocumentos;
}
}
package model;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
#Entity
#Table(name = "pais")
public class Pais implements Serializable {
#Id
#Column(name = "id_pais")
private Integer codigo;
#Column(length = 4)
private String sigla;
private String nome;
#OneToMany(mappedBy = "pais")
private List<UnidadeFederacao> unidadesFederacao;
public Pais() {
}
public Object getId() {
return nome;
}
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
public String getSigla() {
return sigla;
}
public void setSigla(String sigla) {
this.sigla = sigla;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public List<UnidadeFederacao> getUnidadesFederacao() {
return unidadesFederacao;
}
public void setUnidadesFederacao(List<UnidadeFederacao> unidadesFederacao) {
this.unidadesFederacao = unidadesFederacao;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Pais))
return false;
Pais other = (Pais) obj;
if (codigo == null) {
if (other.codigo != null)
return false;
} else if (!codigo.equals(other.codigo))
return false;
return true;
}
}

Resources