XPages - Lotus Domino Java - getDocumentByKey - java

In a Java class in my XPages application, I'm trying to get a handle on a Notes Document in a Notes View. The Notes View contains several Notes Documents. To get the Notes Document I want, I use 2 keys. This produces an error. If I use just one key, the first Notes Document in the Notes View is returned. The Notes View contains two sorted columns. The first column contains the empLang value, the second column contains the templateType value. Here is my code:
String empLang = "en";
String templateType = "C";
Database dbCurr = session.getCurrentDatabase();
String viewName = "vieAdminTemplates" + empLang;
View tview = dbCurr.getView(viewName);
Vector viewKey = new Vector();
viewKey.addElement(empLang);
viewKey.addElement(templateType); // this line causes the code to fail
Document templateDoc = tview.getDocumentByKey(viewKey);
What could be the cause of this problem?

A couple of ideas
1) You could concatenate the key into a single column since you said that worked. Something like 'en~C'
2) You could use the database.search method where you include a string of formula language that isolates the document you want. It returns a collection, and then you pull the document from there.

getDocumentByKey works with multiple columns. There's a known problem with doubles, but you're not hitting that there. One thing that stands out is the second column is just a single letter. That could be considered as a Char instead of a String, either when you do addElement or by the view.
I'd recommend debugging out what data type they are. viewKey.get(1).getClass().getName() I think gives you the class it's stored as. Doing the same for the View Column value.
When you say it causes the code to fail, how does it fail? Does it just not return anything or throw an error?
My next step would be to try testing it where the View and the Vector contain more than one character, e.g. "CC", to help check if there's an underlying issue with Java getDocumentByKey and single characters.

I'm very sorry. The problem here is that the view name in the code is incorrect. There is a view "vieAdminTemplates" but it does not have a second column containing the value "C". With the correct view, the code works fine. Thanks for taking the time to respond to my question.

Related

docx4j / xlsx4j : create simple spreadsheet

I want to create a simple spreadsheet in docx4j / xlsx4j. It shall contain only Strings, no formular is needed. The porpuse is basically switching from a CSV to XLSX
Therefore I tried the example here: https://github.com/plutext/docx4j/blob/master/src/samples/xlsx4j/org/xlsx4j/samples/CreateSimpleSpreadsheet.java
Unfortunetly it is not working. Even after removing the deprecated parts ( http://pastebin.com/bUnJWmFD ).
Excel reports unreadable content and suggest a repair. After that I get the error: "Entfernte Datensätze: Zellinformationen von /xl/worksheets/sheet1.xml-Part". It means something like "removed datasets: Cellinformation at /xl/worksheets/sheet1.xml-Part".
This error occures when createCell is called in line 58 (see. Github, not pastebin) or cell.setV is called with "Hello World" instead of "1234"
I think you are raising 2 issues here:
the resulting XLSX needing repair: this was the result of a typo in cell2.setR, fixed at https://github.com/plutext/docx4j/commit/7d04a65057ad61f5197fb9a98168fc654220f61f
calling setV with "Hello World", you shouldn't do that. Per http://webapp.docx4java.org/OnlineDemo/ecma376/SpreadsheetML/v.html
This element expresses the value contained in a cell. If the cell
contains a string, then this value is an index into the shared string
table, pointing to the actual string value. Otherwise, the value of
the cell is expressed directly in this element. .. For applications
not wanting to implement the shared string table, an 'inline string'
may be expressed in an <is> element under <c> (instead of a
<v> element under <c>),in the same way a string would be
expressed in the shared string table.
though I guess our setV method could detect misuse and either throw an exception or do one of those other things instead.
The CreateSimpleSpreadsheet sample as it stands shows you how to set an inline string, so you just need to test whether your input is a number or not.

Couchbase get the documents for given date range with key

This is one of the sample document which I saved in my bucket,
{
"id": "639882607203778560",
"text": "How does Andy Reid describe the no WR touchdown stat?",
"name": "chiefs",
"createdAt": 1441394876000,
}
I need to fetch the documents for given name and the date range. So this is the view I created for it,
function (doc, meta) {
if (doc._class == "com.link.data" && doc.createdAt && doc.name) {
emit([doc.createdAt,doc.name], null);
}
}
This will give me the all documents for given date range but it doesn't filter based on name. I have all the documents with other names also. How can I achieve this? Also what is the correct implementation for java?
This is the my current impl and I want to do this without using N1ql.
query.setRange(ComplexKey.of(1441376400000L, name), ComplexKey.of(1441396800000L,name));
I tried to add range as a startKey and endKey.Then put the name as a key in couchbase UI and it doesn't work.
Disclaimer: I dont have a lot of experience with compound/complex keys in CB.
I believe what you're asking for cannot be done with a single view: Sorting on date and then filtering on a specific name. You can sort on range and then group-by name, but you'd still get all the various names in the bucket (as you've already noticed).
What you can do is use two separate views and then intersect the results: get the doc-ids with the name you want, get the docs in the range you want and find intersections in your java code. Since views are only 'eventually consistent', your results are just as good as with a single view request, so the only thing you're wasting here is bandwidth and a little time, but not result-precision.
"given" means exact match?
If you want to get result with exact matched name with date range, try emit() with:
emit([doc.name, doc.createdAt], null);
the key array itself, 1st arg in emit() function, is the sort order.

Choices for combobox in a repeat

I've got a view that contains documents with various questions I want answered about Purchase Orders.
Using a repeat, I list all the questions. There are a few different kinds of questions, so I only render the answer field that I need based on the FieldType column value. I want to pull the choices for a combobox from the DialogChoices field on the question document.
I'm currently getting the choices showing as plain text on the next line after the empty combobox instead of as the selectItems. Where is my code going wrong?
<xp:comboBox id="comboBox1">
<xp:this.rendered><![CDATA[#{javascript:rowData.getColumnValue("FieldType") == "Dialog Box"; }]]></xp:this.rendered>
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
var choicesVector:java.util.Vector= doc.getItemValue("DialogChoices");
var choices = [];
// loop through the vector, doing push into the array
for (i=0; i<choicesVector.size(); i++) {
choices.push (choicesVector.elementAt(i))
};
return choices;}]]>
</xp:this.value>
</xp:selectItems>
</xp:comboBox>
Strange, but a test database with the code above does not seem to give me strange results. Maybe it is because the data is in fact not an Vector but just a string?
Here are some tips :
The first thing you could change in your code is the loop to get all the data out of your field. Since the value property of a combobox already expects an array or vector you can change the code to something like:
<xp:this.value><![CDATA[#{javascript:var doc:NotesDocument = rowData.getDocument();
return doc.getItemValue("DialogChoices");
}]]>
</xp:this.value>
But it would be even better to remove the getDocument call at all. If possible you can add a column to the view are you are using for the repeat's datasource. In this column you get the data from the field directory. This way you can use the viewentry's getColumnValue() which is a performance optimization. Something like:
<xp:selectItems>
<xp:this.value><![CDATA[#{try{
return rowData.getColumnValue("DialogChoices");
}catch(e){// do something }]]>
</xp:this.value>
</xp:selectItems>

Java Byte[] to String conversion dropping end quotes / weird side-effect

I am currently trying to perform some regex on the result of a DatagramPacket.getData() call.
Implemented as String myString = new String(thepkt.getData()):
But weirdly, java is dropping the end quotation that it uses to encapsulate all data(see linked image below).
When I click the field in the variable inspector during a debug session and don't change anything, when I click off the variable field it corrects itself again without me changing anything. It even highlights the variable inspection field in yellow to signal change.
Its values are also displaying like it is still a byte array rather than a String object
http://i.imgur.com/8ZItsZI.png
It's throwing off my regex and I can't see anything that would cause it. It's a client server simulation and on the client side, the getData returns the data no problem.
I got it working by using the solution provided in:
https://stackoverflow.com/a/8557165/1700855
But I still don't understand how not specifying the length of the packet to the String constructor would cause it to drop the systematic end double quotes. Can anyone provide an explanation as I really like to understand solutions to my issues before moving on :)
The problem is that you didn't read the spec for DatagramPacket.getData:
Returns the data buffer. The data received or the data to be sent
starts from the offset in the buffer, and runs for length long.
So, to be correct, you should use
new String(thepkt.getData(), thepkt.getOffset(), thepht.getLength())
Or, to not use the default charset:
new String(thepkt.getData(), thepkt.getOffset(), thepht.getLength(), someCharset)

reading multiple lines in file upload

can anyone tell me how to read multiple lines and store their value.
eg:file.txt
Probable Cause: The network operator has issued an alter attribute command for
the specified LCONF assign. The old value and the new value are show
Action Taken : The assign value is changed from the old value to the new
value. Receipt of this message does not guarantee that the new attribute
value was accepted by clients who use it. Additional messages may be.
Probable Cause: The network operator has issued an info attribute command for
the specified LCONF assign. The default value being used is displaye
Action Taken : None. Informational use only.
In the above file, Probable Cause and Action Taken are the column of a database table. And after Probable Cause: those are the value to be stored in the database table for probable cause column, same goes with action taken.
So how can i read the multiple lines and store their value? I have to read the value for probable cause until the line comes with Action Taken. I'm using BufferedReader and the readLine() method to read one line at a time. So can anyone tell me how to read directly from probable cause to action taken no matter how many line comes between them.
The simplest way is probably to just keep a List<String> for each value, with loops something like this:
private static final String ACTION_TAKEN_PREFIX = "Action Taken ";
...
String line;
while ((line = reader.readLine()) != null)
{
if (line.startsWith(ACTION_TAKEN_PREFIX))
{
actions.add(line.substring(ACTION_TAKEN_PREFIX))
// Keep reading the rest of the actions
break;
}
causes.add(line);
}
// Now handle the fact that either we've reached the end of the file, or we're
// reading the actions
Once you've got a "Probable Cause" / "Actions Taken" pair, convert the list of strings back to a single string, e.g. joining with "\n", and then insert in the database. (The Joiner class in Guava will make this easier.)
The tricky bit is dealing with anomalies:
What happens if you don't start with a Probable Cause?
What happens if one probable cause is followed by another, or one set of actions is followed by another?
What happens if you reach the end of the file after reading a probably cause but no list of actions?
I don't have the time to write out a complete solution now, but hopefully the above will help to get you going.

Categories

Resources