How do you refresh document1 from the backend document - xpages

I have a process on the beforePageLoads that executes if it is a newDocument. It grabs a profile docuemnt that contains a number of fields that I need to copy into the XPage that is being loaded. I use the following code:
var iCol:Array = pDoc.getItems()
for(var i=0; i<iCol.length; i++){
var item:NotesItem = iCol[i];
var iName:String = item.getName();
if (#Left(iName, 2) == "AC" ){
iCol[i].copyItemToDocument(doc,"");
}
item.recycle()
} // for loop
where pDoc is the profile document and doc is backend document obtained by var doc = document1.getDocument(). I can then use the copyItemTODocument method and this works real well except I need to refresh the dataSource from the backend document. I can do this from a button and do a partial refresh but that is not an option in a production situation. I have tried various refresh options (suggested in this forum) but none of them get the job done. I can copy the values from the profile document fields to a filed in the datasource but this gets really messy because of data types. I believe my refresh problem is related to updating the doc not document1 in my code. Is there a way to refresh document1 from the backend document?

Does this help ..
http://xpagesblog.com/XPagesHome.nsf/Entry.xsp?documentId=84329CA285163DDF852578CB00669143
it hints at two things whcih may be osf use :
"It appears that during the initial PageLoad events the existing data is transferred from the NotesDocument to the NotesXspDocument. Only those fields bound to the NotesXspDocument (on XPage or CC) are transferred. "
and in the comments section:
"Another way of getting data from the XspDocument to the NotesDocument without a save, is to use document1.getDocument(true) which basically re-syncs the data between the two objects.
I don't have time to experiment I'm afraid but it looks like this is along the right lines, espcially the second part.
Doug

Related

Xpage - Copy document to another document

I have two documents; document1 allows read/write, but document2 is readonly.
var document1:NotesView = database.getView("viewDoc1");
var document2:NotesView = database.getView("viewDoc2");
//var copiedDoc:NotesDocument=document2.CopyToDatabase(document1);
docEv:NotesDocument = document1.getDocumentByKey("userName");
if(docEv!=""){...}else{...};
beforePageLoad
Before page is rendered, I want to be able copy all data in the readonly document and save it into the read/write document and also check if documents already exist in the read/write, in which case don't copy.
Your help will be appreciated.
This line of your code makes no sense:
var copiedDoc:NotesDocument=document2.CopyToDatabase(document1)
First of all, you said that you want to copy document2 into document1, but you appear to be trying to copy it into a new, third NotesDocument called copiedDoc.
But more importantly, you're passing document1 as the argument into the CopyToDatabase method, but that method takes a NotesDatbase argument, not a NotesDocument argument!
You may want to look at the CopyAllItems method instead.

Retrieving app txt data! CoreData Swift

I have made a swift app that logs data into a sqlite file and it consists of 5 txt entry's and when someone enters data into them all and press's save, they will be saved into my sqlite file!
But this issue is when I want to retrieve data! I was following a tutorial on youtube on how to use coredata and save and retrieve inputed data into some text fields. But when he wrote the retrieve code, it only retreived the last known input! But I was wondering if there is anyway I could use only one of the textboxs info to then bring up all the rest of the info in all 5 text fields and Press Retrieve and it will bring it all up! Sort of like a password and then when correct it shows the data that was saved under that password!
Sorry this has been a bit long winded and before you ask, here is the code I used http://pastebin.com/BEq496tY
Thanks,
George Barlow
First, rename your entity from ID to something else, and the attribute id to something else. "ID" is used for so many things, you are likely to run into a problem. I will call your entity Item and the id idNumber.
Second, when saving your object, you take the stuff from the text fields and put it into the appropriate place, e.g.
item.txt1 = textField1.text
item.txt2 = textField2.text
// etc.
Similarly, when retrieving, you should populate all text fields.
textField1.text = item.txt1
textField2.text = item.txt2
// etc.
To have some kind of search functionality you would have to first find out which field is filled with data and then search for that with a predicate, like
NSPredicate(format: "txt1 = %#", textField1.txt)
To create new items you would do something like
let item = NSEntityDescription. insertNewObjectForEntityForName("Item",
inManagedObjectContext:self.managedObjectContext)
// configure item
self.managedObjectContext.save(nil)
That's all there is to it.

How can I read the translation labels from profile documents in XPages?

I am xpages enabling an old Notes application which is using profile documents to store translated labels. The translated lables in the notes form are read from the profile document using #GetProfileField depending on which language the user have selected in their profile.
I have read that profile documents are not recommended to use with xpages so I need a better solution for my xpages users. but it is important that users using Notes client still use the "old" profile document solution.
How can I provide these translation lables to my xpages users?
Thanks
Thomas
In addition to Knut's answer there is also the option to "double" your translated labels via the way to prefer in XPages dev by using the localization options as described here: http://www-10.lotus.com/ldd/ddwiki.nsf/dx/UsingLocalizationOptions.htm
You need to split the task into two. First have a function that is called inside the XPage to get the label you are looking for, secondly have a way to provide that value inside the function.
Making a direct call to the profile isn't a good idea since it fixes the way you provide the data (besides potentially creating a memory leak if you don't recycle dilligently). I would see 4 potential solutions:
Define your profile document as additional data source and simply bind the labels to items in the document. Saves you most of the recycling work, but couples tight
Use a SsJS function: getLabel(name). It would check for a scope variable (a Map) and if not found load it - currently from your profile. If application scope is good enough, you touch the profile once only- speed. If you change the loader later on - you don't need to change anything in the XPage.
Use a managed bean. Same approach as #2, only now you can use el data binding. Your bean needs to implement Map
If the labels hardly change do a design time conversion and write the profile doc out into properties files (works nicely with ODP) and use XPages internal mechanism for internationalization
Let us know how it goes
You can use profile documents for this use case as the content gets changed only with new versions of your project probably. So, you can easily live with profile document's caching.
You get the label translation from a profile document with
var doc = database.getProfileDocument("LabelsEnglish", "");
var label = doc.getItemValueString("label1");
doc.recycle();
return label;
You could read all labels in an application scope variable Map too and do your own caching. This way profile documents would get read only once.
if (!applicationScope.labels) {
var map = new java.util.HashMap();
var doc = database.getProfileDocument("LabelsEnglish", "");
var allItems = doc.getItems();
for (var i = 0; i < allItems.size(); i++) {
var item = allItems.elementAt(i);
item.getName();
map.put(item.getName(), item.getValueString());
item.recycle();
}
doc.recycle();
applicationScope.labels = map;
}
Execute the SSJS code above in a custom control which is included in every XPage (e.g. application layout custom control) in before page load event so you can be sure application scope variable "labels" is initialized when you want to use it. You can access the labels easily with EL
applicationScope.labels.label1

Should I be able to pass a NotesDocumentCollection in a sessionScope variable?

I have one page that does a NotesDatabase search and of course returns a collection. I am storing that collection in a sessionScope variable and calling a page where I what ti display the results.
if ( collection1.getCount() == 0)
{
displayErrorMsg("Your search returned zero results. Please try another search.",getClientId("title"), "Zero Results");
}
else
{
sessionScope.put("searchResults",collection1);
var extCont = facesContext.getExternalContext();
extCont.redirect("xp_vwIssueSearchResults.xsp");
}
The page has a data table with the content of the scope variable as it's datasource:
sessionScope.get("searchResults");
When I call this page I get, NotesException: Object has been removed or recycled.
Should I be able to pass a NotesDocumentCollection in a sessionScope variable? I have some thoughts on work arounds but it would sure be nice to pass the NotesDocumentCollection.
You can't keep Notes objects like views, documents, collections for a longer time. In general, you should work with Notes object in a way, that you save the data you're interested in somewhere and recycle (=destroy) the Notes objects right away. The reason is that the Domino server can't save the Notes objects for you between two request because Notes objects aren't serializable (serializable = ability to save objects on disk and restore them back to memory).
In your case, you could save
the search query or
the result values as an Array of Objects (JavaScript) or a List of Maps (Java) or
the documentUniqueIDs of document collection's documents
in your sessionScope variable and use them in your redirected page.

Use datasource to get backend document

I got a panel which bind to an open document as data source called document1 in an extlib dialog box. On button save, I want to compare all the field values between back-end document and document1. However, all the field values from back-end document are seemed to be updated therefore their field values are the same in document1. The comparison is done before docuemnt1.save().
From my understanding, document1.getDocument() should get the back-end document which all original/current data. document1.getDocument(true) should get all new data. I had try to getDocumentByID and found that all field values are updated in back-end document. I have no idea because document1.save not yet executed.
Why back-end document being updated with new data before save?
Is there any better way to get back-end document which all original/current data?
If you are using the the parameter true with the getDocument method, all changes done to the datasource are temporarly written to the datasoure's datastore.
The XPages engine is "smart enough" to realize that multiple instances of a NotesDocument object are all referencing the same backend document. It will now return the cached data from the datasource's datastore. That is why all objects will now return the updated values instead of the values stored in the backend document (Using of multiple datasources will give you the same result).
To access the data from the backend document, you can use a #DbLookup on a view with all documents sorted by their UNID.
#DbLookup("","AllByUNID", document1.getDocument( true ).getUniversalID(), "FIELD")

Resources