I am new to MVC and following this link I have a search page for resulting pdf metadatas by using Solr. My if statement and for loop in html side do not work
Searching.java in models folder:
public class Searching {
public String q;
public String outputTitle;
public String outputAuthor;
public String outputContent;
public String outputPage;
public String outputPath;
}
search function in Application.java:
final static Form<Searching> searchForm = form(Searching.class);
final static List<Searching> searchList = new ArrayList<Searching>();
public static Result search() {
Form<Searching> filledForm = searchForm.bindFromRequest();
Searching searched = filledForm.get();
....(database connection lines)
QueryResponse response = solr.query(query);
SolrDocumentList results = response.getResults();
if(results.isEmpty())
System.out.println("SEARCH NOT FOUND");
else {
for (int i = 0; i < results.size(); ++i) {
searched.outputTitle = (String)results.get(i).getFirstValue("title");
searched.outputAuthor = (String)results.get(i).getFirstValue("author");
searched.outputPage =results.get(i).getFirstValue("pageNumber").toString();
searched.outputContent = (String)results.get(i).getFirstValue("content");
searched.outputPath = (String)results.get(i).getFirstValue("path");
searchList.add(searched);
}
System.out.println("\nresults.getNumFound(): "+ searched.outputFound);
System.out.println("results.size(): "+results.size());
}
return play.mvc.Results.ok(search.render(searched, searchForm, searchList));
}
search.scala.html
#(searched: Searching, searchForm: Form[Searching], searchList: List[Searching])
.. some buttons,a search bar...
#if(searchList.isEmpty()) {
<h1>Error</h1>
} else {
#for(search <- searchList) {
<ul>Title: #search.outputTitle</ul>
<ul>Author: #search.outputAuthor <a href="#search.outputPath" download>Download PDF</a></ul>
<ul>Number of Page(s): #search.outputPage</ul>
}
}
Java code works well. I can see outputs on the terminal, but my html side has problem and it shows one book many times according to size of searchList
I am posting the answer explicitly even though I was able to help the OP in the chat - maybe somebody else is running into such problem but did not check the chat:
The problem is even though you have the line in the for-loop you are still using the same searched variable. What you have to do is to reinitialize the variable when entering the loop. Something like:
for (...) {
searched = new Searching();
searched.outputTitle = (String)results.get(i).getFirstValue("title");
....
searchList.add(searched);
}
This solves the problem with the duplicates and everything is fine now.
Related
I need to extract word document comments and the text they comment on. Below is my current solution, but it is not working as expcted
public class Main {
public static void main(String[] args) throws Exception {
var document = new Document("sample.docx");
NodeCollection<Paragraph> paragraphs = document.getChildNodes(PARAGRAPH, true);
List<MyComment> myComments = new ArrayList<>();
for (Paragraph paragraph : paragraphs) {
var comments = getComments(paragraph);
int commentIndex = 0;
if (comments.isEmpty()) continue;
for (Run run : paragraph.getRuns()) {
var runText = run.getText();
for (int i = commentIndex; i < comments.size(); i++) {
Comment comment = comments.get(i);
String commentText = comment.getText();
if (paragraph.getText().contains(runText + commentText)) {
myComments.add(new MyComment(runText, commentText));
commentIndex++;
break;
}
}
}
}
myComments.forEach(System.out::println);
}
private static List<Comment> getComments(Paragraph paragraph) {
#SuppressWarnings("unchecked")
NodeCollection<Comment> comments = paragraph.getChildNodes(COMMENT, false);
List<Comment> commentList = new ArrayList<>();
comments.forEach(commentList::add);
return commentList;
}
static class MyComment {
String text;
String commentText;
public MyComment(String text, String commentText) {
this.text = text;
this.commentText = commentText;
}
#Override
public String toString() {
return text + "-->" + commentText;
}
}
}
sample.docx contents are:
And the output is (which is incorrect):
factors-->This is word comment
%–10% of cancers are caused by inherited genetic defects from a person's parents.-->Second paragraph comment
Expected output is:
factors-->This is word comment
These factors act, at least partly, by changing the genes of a cell. Typically, many genetic changes are required before cancer develops. Approximately 5%–10% of cancers are caused by inherited genetic defects from a person's parents.-->Second paragraph comment
These factors act, at least partly, by changing the genes of a cell. Typically, many genetic changes are required before cancer develops. Approximately 5%–10% of cancers are caused by inherited genetic defects from a person's parents.-->First paragraph comment
Please help me with a better way of extarcting word document comments and the text they comment on. If you need additional details let me know, I will provide all the required details
The commented text is marked by special nodes CommentRangeStart and CommentRangeEnd. CommentRangeStart and CommentRangeEnd nodes has Id, which corresponds the Comment id the range is linked to. So you need to extract content between the corresponding start and end nodes.
By the way, the code example in the Aspose.Words API reference shows how print the contents of all comments and their comment ranges using a document visitor. Looks like exactly what you are looking for.
EDIT: You can use code like the following to accomplish your task. I did not provide full code for extracting content between nodes, is is availabel on GitHub
Document doc = new Document("C:\\Temp\\in.docx");
// Get the comments in the document.
Iterable<Comment> comments = doc.getChildNodes(NodeType.COMMENT, true);
Iterable<CommentRangeStart> commentRangeStarts = doc.getChildNodes(NodeType.COMMENT_RANGE_START, true);
Iterable<CommentRangeEnd> commentRangeEnds = doc.getChildNodes(NodeType.COMMENT_RANGE_END, true);
for (Comment c : comments)
{
System.out.println(String.format("Comment %d : %s", c.getId(), c.toString(SaveFormat.TEXT)));
CommentRangeStart start = null;
CommentRangeEnd end = null;
// Search for an appropriate start and end.
for (CommentRangeStart s : commentRangeStarts)
{
if (c.getId() == s.getId())
{
start = s;
break;
}
}
for (CommentRangeEnd e : commentRangeEnds)
{
if (c.getId() == e.getId())
{
end = e;
break;
}
}
if (start != null && end != null)
{
// Extract content between the start and end nodes.
// Code example how to extract content between nodes is here
// https://github.com/aspose-words/Aspose.Words-for-Java/blob/master/Examples/src/main/java/com/aspose/words/examples/programming_documents/document/ExtractContentBetweenCommentRange.java
}
else
{
System.out.println(String.format("Comment %d Does not have comment range"));
}
}
I use templ4docx/Apache POI (2.0.3/3.17).
There you can set a VariablePatten like this:
private static final String START_PATTERN = "#{";
private static final String END_PATTERN = "}";
...
targetDocx.setVariablePattern(new VariablePattern(START_PATTERN, END_PATTERN));
When i use a placeholder with dots, it´s not working inside Header/Footer. In the Body with dots it works. And Images works too with placeholder and dots inside!
Example in Word-Template:
#{Person.Name} // works in Body NOT in Header/Footer!
#{Person.Name} // works in Body and Header/Footer!
#{Person} // works in Body and Header/Footer!
#{Image.Name} // works in Body and Header/Footer! Here i use ImageVariable instead of Textvariable.
I debug the code an saw the "run.setText()" is called with the right Text but in the final document it´s not.
#Override
public void insert(Insert insert, Variable variable) {
if (!(insert instanceof TextInsert)) {
return;
}
if (!(variable instanceof TextVariable)) {
return;
}
TextInsert textInsert = (TextInsert) insert;
TextVariable textVariable = (TextVariable) variable;
for (XWPFRun run : textInsert.getParagraph().getRuns()) {
String text = run.getText(0);
if (StringUtils.contains(text, textInsert.getKey().getKey())) {
text = StringUtils.replace(text, textVariable.getKey(), textVariable.getValue());
if (text.contains("\n")) {
String[] textLines = text.split("\n");
run.setText(textLines[0], 0);
for (int i = 1; i < textLines.length; i++) {
run.addBreak();
run.setText(textLines[i]);
}
} else {
run.setText(text, 0);
}
}
}
}
Any ideas why it didn´t work with Placeholder "#{Person.Name}" as a TextVariable in Header/Footer? But it works with "#{PersonName}" and ImageVariable "#{Images.Logo1}"???
It looks like Word sometimes separates the placeholders, so you'll only find parts of the placeholder in the runs.
In the class "TextInsertStrategy" i check in the loop of the runs for split placeholders and treat it accordingly. With that I could solve the problem.
I am currently working on a Java program that crawls a webpage and prints out some information from it.
There is one part that I can't figure out, and thats when I try to print out one specific String Array with some information in it, all it gives me is " ] " for that line. However, a few lines before, I also try printing out another String array in the exact same way and it prints out fine. When I test what is actually being passed to the "categories" variable, its the correct information and can be printed out there.
public class Crawler {
private Document htmlDocument;
String [] keywords, categories;
public void printData(String urlToCrawl)
{
nextURL=urlToCrawl;
crawl();
//This does what its supposed to do. (Print Statement 1)
System.out.print("Keywords: ");
for (String i :keywords) {System.out.print(i+", ");}
//This doesnt. (Print Statement 2)
System.out.print("Categories: ");
for (String b :categories) {System.out.print(b+", ");}
}
public void crawl()
{
//Gather Data
//open up JSOUP for HTTP parsing.
Connection connection = Jsoup.connect(nextURL).userAgent(USER_AGENT);
Document htmlDocument = connection.get();
this.htmlDocument=htmlDocument;
System.out.println("Recieved Webpage "+ nextURL);
int guacCounter = 0;
for(Element guac : htmlDocument.select("script"))
{
if(guacCounter==5)
{
//String concentratedGuac = guac.toString();
String[] items = guac.toString().split("\\n");
categories = processGuac(items);
break;
}
else if(guacCounter<5) {
guacCounter++;
}
}
}
public String[] processKeywords(String totalKeywords)
{
String [] separatedKeywords = totalKeywords.split(",");
//System.out.println(separatedKeywords.toString());
return separatedKeywords;
}
public String[] processGuac(String[] inputGuac)
{
int categoryIsOnLine = 6;
String categoryData = inputGuac[categoryIsOnLine-1];
categoryData = categoryData.replace(",","");
categoryData = categoryData.replace("'","");
categoryData = categoryData.replace("|",",");
categoryData = categoryData.split(":")[1];
//this prints out the list of categories in string form.(Print Statement 3)
System.out.println("Testing here: " + categoryData.toString());
String [] categoryList=categoryData.split(",");
//This prints out the list of categories in array form correctly.(Print statement 4)
System.out.println("Testing here too: " );
for(String a : categoryList) {System.out.println(a);}
return categoryList;
}
}
I cut out a lot of the irrelevant parts of my code so there might be some missing variables.
Here is what my printouts look like:
PS1:
Keywords: What makes a good friend, making friends, signs of a good friend, supporting friends, conflict management,
PS2:
]
PS3:
Testing here: wellbeing,friends-and-family,friendships
PS4:
Testing here too:
wellbeing
friends-and-family
friendships
I have verified that the entity I am looking for is in the datastore. I have verified that the list I pass as a method parameter contains this entity. I am trying to find all objects that have their 'userGmail' contained in the list of strings I pass.
Here is my code
#SuppressWarnings("unchecked")
#ApiMethod(name = "findFriendsByEmailList")
public CollectionResponse<ZeppaUser> findFriendsByEmailList(
#Named("emailsList") List<String> emailsList, User user)
throws OAuthRequestException {
if (user == null) {
throw new OAuthRequestException(
"Null User Authorization Exception, findFriendsByEmailList");
}
PersistenceManager mgr = null;
List<ZeppaUser> execute = null;
Query query = null;
try {
mgr = getPersistenceManager();
query = mgr.newQuery(ZeppaUser.class);
query.declareParameters("java.util.List emailListParam");
query.setFilter("emailListParam.contains( userGmail )");
execute = (List<ZeppaUser>) query.execute(emailsList);
query.closeAll();
} finally {
mgr.close();
}
return CollectionResponse.<ZeppaUser> builder().setItems(execute)
.build();
}
This is the stack trace I receive from it:
Something worth noting: I do not receive this error on lists I pass in that to not contain an element found in the datastore. Just when it does exist which leads me to believe that the Query has located the element but has not been closed or executed into a return parameter correctly. If it is preferable to return List that is more than ok. I have tried multiple variations of this with no success thus far. It is getting quite frustrating.
Ok so I found a way around it.
Lists cannot be passed into ApiEndpoints. That or I didn't figure out the correct way to do it and would LOVE an update on the proper way to do this.
Instead, in my client, I construct a String of emails seperated by a comma and send a string into the parameter as an 'encoded' string list then 'decode' it upon execution. Works well but seems hacky.
here are the methods I used. This is convenient though because it works with iOS as well.
public static String encodeListString(ArrayList<String> stringList){
StringBuilder stringbuilder = new StringBuilder();
stringbuilder.append(stringList.get(0));
if(stringList.size() > 1){
for( int i = 0; i < stringList.size(); i++){
stringbuilder.append(",");
stringbuilder.append(stringList.get(i));
}
}
return stringbuilder.toString();
}
public static List<String> decodeListString(String encodedString){
char[] characters = encodedString.toCharArray();
StringBuilder stringbuilder = new StringBuilder();
int position = 0;
ArrayList<String> stringList = new ArrayList<String>();
while(true){
try {
char character = characters[position];
if(character == ','){
String resultString = stringbuilder.toString();
stringList.add(resultString);
stringbuilder = new StringBuilder(); // clear it
} else {
stringbuilder.append(character);
}
position++;
} catch (ArrayIndexOutOfBoundsException aiex){
// List ended
String resultString = stringbuilder.toString();
if(!resultString.isEmpty())
stringList.add(resultString);
break;
}
}
return stringList;
}
I have incorporated my jsp pages with an autosuggest features, and to make the autosuggest functioning, it has to make an array of the data to be suggested using scriplet.
My issues is, as i import the data from oracle 10G, the time to complete the import of the data into the pages, is about 20-40 second.
Hence, is there any way to increase the speed of data transfer i already try the indexing method but it does help with the time, and the number of data in the table is about 6K .
Can the oracle pro, suggest any ways to solve this issues.. hehe =D
And here is the sample of my program :-
Function in my autosuggest.java, to retrieve and filter the data from my db.
public List getVendorName() throws Exception
{
List temp=null;
Database db=null;
String tempVar, tempVar2;
boolean tempVar3, tempVar4;
int counter=0;
try {
db = PersistenceHelper.beginTransaction();
JDOQuery queryFund = new JDOQuery(db,Trader.class);
queryFund.setFilter(PersistenceHelper.getOrganizationFilter("organization"));
temp = new ArrayList();
QueryResults results = queryFund.execute();
while(results.hasMore())
{
Trader trader = (Trader) results.next();
tempVar = "\""+trader.getName()+"\"";
tempVar2 = trader.getTraderType();
tempVar3 = trader.getRegTraderStatus();
tempVar4 = trader.getMainTraderStatus();
if(!tempVar2.equalsIgnoreCase("c"))
{
if( (tempVar3 == true) && (tempVar4 == true))
{
tempVar = tempVar.replace("\n", "");
temp.add(counter,tempVar); counter++;
}
}
}
}
catch (Exception e) {
System.out.println(e);
}
finally{
PersistenceHelper.closeTransaction(db, false);
}
return temp;
}
Function in my jsp page. to retrieve the data from autosuggest.java;
function getVendorName()
{
var temp = new Array();
<%
AutoSuggest as = new AutoSuggest();
List tempList = as.getVendorName();
for(int i=0; i<tempList.size(); i++)
{
Strinmg tempVar = (String) tempList.get(i);
%> temp[<%=i%>] = <%=tempVar%>; <%
}
%>
return temp;
}
The first thing to try would be to do the filtering by trader type and status in the database query ... rather on the Java side. The way you are currently doing this will entail pulling large numbers of Trader objects from the database. I expect most of them are then being discarded by your Java code.