Vaadin access content of sql container - java

EDITED!
I retriev data from a mysql DB using Vaadin, SQLContainer and the Freeformquery. Now I want to get all Descriptions in one String (later each string will printed/added to a text file).
....
String text ="";
FreeformQuery subcatExtractionQuery = new FreeformQuery("select Description from customers", connectionPool);
SQLConateiner s = new SQLContainer(subcatExtractionQuery);
Collection<?> c = s.getContainerPropertyIds();
for(Object o : c){
Property<?> p = s.getContainerProperty(o, "Description");
text+=(String)p.getValue();
}
System.out.println(text);
I get the Error: java.lang.NullPointerException
Problem line is the text+=...if I remove it, no error appears. BUT The Query returns Data!
These descriptions are Strings. I need to have all words of each string in ony single list of tokens (i already have a method to create a token list of a file.)
(Don't ask if this makes sense, it's just an example).
How can I access the Strings of my sql container? Till now I only used the container als data source of a table and didn't access the single items/strings.
I need a for loop to get each String...how does this work?

Its getItemIds insted of getContainerPropertyIds. So this words
....
String text ="";
FreeformQuery subcatExtractionQuery = new FreeformQuery("select Description from customers", connectionPool);
SQLConateiner s = new SQLContainer(subcatExtractionQuery);
Collection<?> c = s.getItemIds();
for(Object o : c){
Property<?> p = s.getContainerProperty(o, "Description");
text+=(String)p.getValue();
}
System.out.println(text);

Related

Insert data into mysql table in proper format from java servlet

I am getting cart items and cart price from cart javascript to jsp and then transferring same from jsp to servlet as follows:
String a= request.getParameter("cartitems");//but it prints like Jan-2019Feb-2019March-2019
String b=request.getParameter("cartprice");//prints like $100,$200,$400
I need to store data in my database as follows:
Cart Price
Jan-2019 $100
Feb-2019 $200
March-2019 $400
I am trying to separate this Jan-2019Feb-2019March-2019 so that it can be stored in my table but unable to do so, Jan-2019Feb-2019March-2019 need not to be separated by date format, it can be separated in any suitable regex if possible.
I tried to separate string a as follows but its not working.
String[] result = a.split(",");
out.println(Arrays.toString(result));// prints [June-2019March-2019]
How can I store datas like above in my databse table, please help ! Many thanks in advance!
Get parameter from your servlet request:
String cartItemsParameter= request.getParameter("cartitems");
use mehod:
findRexExpList(cartItemParameter, "\\D+\\d+");
method code:
private static List<String> findRexExpList(String text, String regExp) {
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
String group = matcher.group();
result.add(group);
}
return result;
}
this method return List with separated string
For store data:
List<String> carts = findRexExpList(cartItemParameter, "\\D+\\d+");
String[] cartPrices = request.getParameter("cartprice").split(",");
for(int i=0; i<cartPrices.length; i++){
String cart = carts.get(i);
String price = cartPrices[i];
//insert cart and price
}

How to store HQL result(list) into a String Variable in SpringMVC & Hibernate?

For password changing form ,require to match old password with new password.
I wants to retrieve Password from database as per username and store this password into an String variable to compare with new password.Or Is there any method in spring to compare this?
String password = "";
Query que = session.getCurrentSession().createQuery(selectpassword);
List <String>list = que.list();
for (String srt : list) {
password += srt;
}
System.out.println("Password ->" +list);
Result is like java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.lang.String
How to solve this?I wants to Store this query result into String Variable.
If your query is select password from table. Then hibernate Must be returning List where password index must be 0.
So U can say
List <Object>list = que.list();
ArrayList passwordList = (ArrayList)list.get(0)
List<String> passwords = new ArrayList<String>();
for(ArrayList list :passwordList){
passwords.add(list.toString());
}

Getting original text after using stanford NLP parser

Hello people of the internet,
We're having the following problem with the Stanford NLP API:
We have a String that we want to transform into a list of sentences.
First, we used String sentenceString = Sentence.listToString(sentence); but listToString does not return the original text because of the tokenization. Now we tried to use listToOriginalTextString in the following way:
private static List<String> getSentences(String text) {
Reader reader = new StringReader(text);
DocumentPreprocessor dp = new DocumentPreprocessor(reader);
List<String> sentenceList = new ArrayList<String>();
for (List<HasWord> sentence : dp) {
String sentenceString = Sentence.listToOriginalTextString(sentence);
sentenceList.add(sentenceString.toString());
}
return sentenceList;
}
This does not work. Apparently we have to set an attribute " invertible " to true but we don't know how to. How can we do this?
In general, how do you use listToOriginalTextString properly? What preparations do you need?
sincerely,
Khayet
If I understand correctly, you want to get the mapping of tokens to the original input text after tokenization. You can do it like this;
//split via PTBTokenizer (PTBLexer)
List<CoreLabel> tokens = PTBTokenizer.coreLabelFactory().getTokenizer(new StringReader(text)).tokenize();
//do the processing using stanford sentence splitter (WordToSentenceProcessor)
WordToSentenceProcessor processor = new WordToSentenceProcessor();
List<List<CoreLabel>> splitSentences = processor.process(tokens);
//for each sentence
for (List<CoreLabel> s : splitSentences) {
//for each word
for (CoreLabel token : s) {
//here you can get the token value and position like;
//token.value(), token.beginPosition(), token.endPosition()
}
}
String sentenceStr = sentence.get(CoreAnnotations.TextAnnotation.class)
It gives you original text. An example for JSONOutputter.java file :
l2.set("id", sentence.get(CoreAnnotations.SentenceIDAnnotation.class));
l2.set("index", sentence.get(CoreAnnotations.SentenceIndexAnnotation.class));
l2.set("sentenceOriginal",sentence.get(CoreAnnotations.TextAnnotation.class));
l2.set("line", sentence.get(CoreAnnotations.LineNumberAnnotation.class));

Mapping several columns from sql to a java object

I am trying to retrieve and process code from JIRA, unfortunately the pieces of information (which are in the Metadata-Plugin) are saved in a column, not a row.
Picture of JIRA-MySQL-Database
The goal is to save this in an object with following attributes:
public class DesiredObject {
private String Object_Key;
private String Aze.kunde.name;
private Long Aze.kunde.schluessel;
private String Aze.projekt.name;
private Long Aze.projekt.schluessel
//getters and setters here
}
My workbench is STS and it's a Spring-Boot-Application.
I can fetch a List of Object-Keys with the JRJC using:
JiraController jiraconnect = new JiraController();
List<JiraProject> jiraprojects = new ArrayList<JiraProject>();
jiraprojects = jiraconnect.findJiraProjects();
This is perfectly working, also the USER_KEY and USER_VALUE are easily retrievable, but I hope there is a better way than to perform
three SQL-Searches for each project and then somehow build an object from all those lists.
I was starting with
for (JiraProject jp : jiraprojects) {
String SQL = "select * from jira_metadata where ENRICHED_OBJECT_KEY = ?";
List<DesiredObject> do = jdbcTemplateObject.query(SQL, new Object[] { "com.atlassian.jira.project.Project:" + jp.getProjectkey() }, XXX);
}
to get a list with every object, but I'm stuck as i can't figure out a ObjectMapper (XXX) who is able to write this into an object.
Usually I go with
object.setter(rs.getString("SQL-Column"));
But that isn't working, as all my columns are called the same. (USER_KEY & USER_VALUE)
The Database is automatically created by JIRA, so I can't "fix" it.
The Object_Keys are unique which is why I tried to use those to collect all the data from my SQL-Table.
I hope all you need to enlighten me is in this post, if not feel free to ask for more!
Edit: Don't worry if there are some 'project' and 'projekt', that's because I gave most of my classes german names and descriptions..
I created a Hashmap with the Objectkey and an unique token in brackets, e.g.: "(1)JIRA".
String SQL = "select * from ao_cc6aeb_jira_metadata";
List<JiraImportObjekt> jioList = jdbcTemplateObject.query(SQL, new JiraImportObjektMapper());
HashMap<String, String> hmap = new HashMap<String, String>();
Integer unique = 1;
for (JiraImportObjekt jio : jioList) {
hmap.put("(" + unique.toString() + ")" + jio.getEnriched_Object_Key(),
jio.getUser_Key() + "(" + jio.getUser_Value() + ")");
unique++;
}
I changed this into a TreeMap
Map<String, String> tmap = new TreeMap<String, String>(hmap);
And then i iterated through that treemap via
String aktuProj = new String();
for (String s : tmap.keySet()) {
if (aktuProj.equals(s.replaceAll("\\([^\\(]*\\)", ""))) {
} else { //Add Element to list and start new Element }
//a lot of other stuff
}
What I did was to put all the data in the right order, iterate through and process everything like I wanted it.
Object hinfo = hmap.get(s);
if (hinfo.toString().replaceAll("\\([^\\(]*\\)", "").equals("aze.kunde.schluessel")) {
Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(hinfo.toString());
while (m.find()) {
jmo[obj].setAzeKundeSchluessel(Long.parseLong(m.group(1), 10));
// logger.info("AzeKundeSchluessel: " +
// jmo[obj].getAzeKundeSchluessel());
}
} else ...
After the loop I needed to add the last Element.
Now I have a List with the Elements which is easy to use and ready for further steps.
I cut out a lot of code because most of it is customized for my problem.. the roadmap should be enough to solve it though.
Good luck!

docx4j copy image to a new document

I am trying to use docx4j to make partial copy of the document to a new document. I am able to copy most part - text, format etc. However, in case of image, I am not able to copy. Doing a deep copy for inline images leave the document incorrectly formatted, and same happens for linked images.
I am attaching code used to make copy
word = WordprocessingMLPackage.load(new File("C:\\Users\\prerak\\Documents\\Projects\\EME\\Exam System\\Documents\\T.docx"));
//newDoc = WordprocessingMLPackage.createPackage();
newDoc = WordprocessingMLPackage.load(new File("C:\\Users\\prerak\\Documents\\Projects\\EME\\Exam System\\Documents\\T.docx"));
MainDocumentPart mdp = word.getMainDocumentPart();
newDoc.getMainDocumentPart().getContent().clear();
Document contents = mdp.getContents();
Body body = contents.getBody();
List<Object> content = body.getContent();
ArrayList<ArrayList<Object>> allQ = new ArrayList<>();
ArrayList<Object> next = null;
for (Object o : content) {
if (o instanceof P) {
P p = (P) o;
List<Object> rs = DocxUtils.getAllElementFromObject(p, R.class);
for(Object d:rs){
R tt = (R) d;
List<Object> ds = tt.getContent();
for(Object dd:ds){
System.out.println(dd.getClass().getName());;
}
}
PPr ppr = (PPr) p.getPPr();
if (ppr != null && ppr.getPStyle() != null) {
System.out.println("Style: " + ppr.getPStyle().getVal());
if (ppr.getPStyle().getVal().equals("Heading1")) {
//System.out.println(o.toString());
}
}
}
if (o.toString().startsWith("##")) {
next = new ArrayList<>();
allQ.add(next);
}
if (next != null) {
next.add(o);
}
}
//System.out.println("Total number of questions " + allQ.size());
for (Object o : allQ.get(0)) {
newDoc.getMainDocumentPart().getContent().add(XmlUtils.deepCopy(o));
}
//System.out.println(DocxUtils.paraToHtml(allQ.get(0)));
newDoc.save(new File("C:\\Users\\prerak\\Documents\\Projects\\EME\\Exam System\\Documents\\newt90.docx"));
Do I have to do anything more than making a deep copy?
Your help is greatly appreciated.
Thanks
See http://www.docx4java.org/blog/2010/11/merging-word-documents/ regarding referential integrity.
The "brute force" approach is to make a copy of the WordprocessingMLPackage using WordprocessingMLPackage's clone() method, then delete the content you don't want. That'll leave the image reference and its corresponding part, but also other images etc which you aren't using anymore. Depending on your circumstances that may or may not be ok. If it isn't, the commercial MergeDocx code is one solution.
Alternatively, if you know the only references you have are say, images (as opposed to comments, footnotes etc etc), you could handle those specifically in your code.

Categories