TeamSpace
Quality vs. quantity Microphone
Home Who We Are What We Do What We've Done Why Work For Us Blog Contact Us

My Managed Bean Conundrum - Part 2

Bob Laing - April 22 2013 10:00:00 AM


Continuing on from Part 1 in this series it is now enough of copying someone else's code -- it's time to write my own code.  I'm going to create a bean called AppUtils - the purpose will be store code that will be portable to other databases/applications and provide generic methods to access and retrieve data from Domino objects (typically documents).  To create this bean I'll follow much the same process as when creating the SystemUtils bean.  From Designer under Code and then Java I click New Java Class and fill in the Package of com.teamspace.ca and the Name of AppUtils and then click Finish.  This creates an empty Java class as in the following screen shot:

Image:My Managed Bean Conundrum - Part 2

The next step is to make the bean serializable.  This means the bean can have persistence, or able to store information to be retrieved at a later point.  To enable serialization, add the text "implements Serializable" after public class Apputils as in the following screen shot:

Image:My Managed Bean Conundrum - Part 2

Notice the red x on the left side.  Click once on that and you'll see an explanation of the error and suggestion on how to resolve.

Image:My Managed Bean Conundrum - Part 2

Double click on Import 'Serializable' (java.io).  This will add an import statement at the top of the bean which imports the Serializable Java Class into this bean.  This will eliminate the hard error that will prevent the  bean from being saved, but now you'll see a yellow warning.  If you click once on this you'll be prompted to Add generated serial version ID.  This is a long unique id number which helps keeps track of version of the bean.

Image:My Managed Bean Conundrum - Part 2

Double click the Add generated serial version ID which will add a long variable with a unique value.  Next we're going to add a constructor.  A constructor has a solitary purpose and that is to create an instance of a class.  Add the code highlighted in the following screenshot.

Image:My Managed Bean Conundrum - Part 2

Now we're done with the preliminary stuff and ready to write some code.  The method I started with for my bean will take as input a lookup value, a lookup view and a field name.  The method will lookup the value in the first column of the view and return the field value from the first document that matches the lookup value.  Copy the following code (not including the line numbers) into the bean after the public AppUtils() {} and I'll explain what it does.

1        public static Vector<String> getKeywordList(String kwCategory, String kwView, String kwField) {
2                Vector<String> kwList = new Vector<String>();
3                try {
4                        Database curDb = SystemUtils.getDb();
5                        if (curDb.isOpen()) {
6                                View kwLookupView = curDb.getView(kwView);
7                                if (null != kwLookupView) {
8                                        Vector<String> v = new Vector<String>();
9                                        v.addElement(kwCategory);
10                                        Document doc = kwLookupView.getDocumentByKey(v, true);
11                                        if (null != doc) {
12                                                kwList = doc.getItemValue(kwField);
13                                        }
14                                }
15                        }
16
17                } catch (NotesException e) {
18                        e.printStackTrace();
19                }
20                return kwList;
21        }


Line 1 identifies the method name which is getKeywordList.  This method is public, which means it is visible outside of this class and it is static which means the method can be called directly.  The method receives three parameters:  kwCategory, kwView and kwField.  All three parameters are identified as String variables.

Line 2 identifies the Vector which will hold the values that will be returned from the method.  A Vector is a Java object that can hold multiple values.  A Vector in this case will return the values contained in a multi-value field in a  Domino document.  The type of the Vector needs to be identified, in this case the Vector will hold values.

Line 3 starts the standard Java try/catch block.  Java tries code, and if it fails for any reason, the processing falls to the catch statement which can take whatever action is necessary.  In this case the catch will print the error message to the console(line 18)

Line 4 uses the SystemUtils bean to get the current database object.  This database object contains the same properties that a database object would contain in Lotusscript

Line 5 checks to verify the database object is valid and open -- looks just like Lotusscript doesn't it ?

Line 6 and 7 get a view object from the current database and checks to see if the view object exists.  The view is obtained using the kwView parameter that was passed to the method.

Line 8 declares a new vector that will be used as the lookup key to get a document in the view.  Line 9 adds the kwCategory value to the vector.

Line 10 and 11 get a document using getDocumentByKey and verify that a document was found.

Line 12 gets the value contained in the kwField and stores it in the kwList vector.  This vector will then be returned from this method in line 20.

I've been coding Java agents in Domino for years so my perspective may be a littie warped, but I see very little in this method that differs from Lotusscript.  The syntax is a little different, but the Domino objects are the same regardless what language is manipulating them.

To see this bean in action, I've created a simple XPage that returns the value retrieved to a combobox control.  That code follows:

<xp:label
      value="getKeywordList Test(Platform KW): "
      id="label3"
      for="comboBox1">
</xp:label>

<xp:comboBox
      id="comboBox1">
      <xp:selectItem
              itemLabel="Select"
              itemValue="Select">
      </xp:selectItem>
      <xp:selectItems>
              <xp:this.value><![CDATA[#{javascript:getAppUtils.getKeywordList("STATUS", "KeywordsLU", "value");}]]></xp:this.value>
      </xp:selectItems>
</xp:comboBox>


In the comboBox, the following code  javascript:getAppUtils.getKeywordList("STATUS", "KeywordsLU", "value"); calls the method getKeywordList and passes as parameters "STATUS" which serves as the lookup key in the "KeywordsLU" view which then gets the matching document and returns the contents of the "value" field in the document.  This then results in the comboBox looking like this:

Image:My Managed Bean Conundrum - Part 2

So all is good right ?  I have a bean, and I can use it in an XPage to retrieve the values I want.  What's the conundrum ???  I was doing some searching and reading on managed beans and I saw a post that mentioned that managed beans contain getters and setters(which my bean does not) and if there are no getters and setters then the managed bean actually is just a plain old Java method.  So do I have a managed bean here or do I just have a Java object ?  I'm really not sure - weigh in if you have an opinion…
Comments

1David Leedy  4/22/2013 10:33:25 AM  My Managed Bean Conundrum - Part 2

I did a show on "Java Beans" a little while back. { Link }

As I said there, my opinion is that to be "Managed" means you're using the faces-config file to activate and "manage" them. Otherwise they're just POJO - Plain Old Java Objects or what I call "unmanaged" beans.

Your getKeyWordList is technically a "getter" I would say. So you do have that.

2Bob Laing  4/22/2013 11:05:18 AM  My Managed Bean Conundrum - Part 2

Thanks for the feedback. Semantics aside, you're right that a value is being returned -- so it would qualify as a getter.

3Tim Tripcony  4/22/2013 12:00:05 PM  My Managed Bean Conundrum - Part 2

The class you currently have is a POJO, as David mentioned; registering it as a managed bean is just a convenient way to have a variable that points to a managed instance of it. But it's not a bean.

A bean is really a depiction of an object's state as a snapshot in time: a series of properties, and methods for inspecting or modifying those properties.

In beans, getters (and setters) are paired with properties. So your class would look something like this:

public class AppUtils implements Serializable {

private String category;

private String view;

private String field;

private Vector<String> keywordList;

public AppUtils() {

/* constructor without arguments to ensure the

bean instance can be created without needing

to know what arguments to pass */

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

public String getView() {

return view;

}

public void setView(String view) {

this.view = view;

}

public String getField() {

return field;

}

public void setField(String field) {

this.field = field;

}

public Vector<String> getKeywordList() {

if (keywordList == null) {

/*

Do what you're already doing, but use the

other properties' getters to determine

category, view, and field instead of

passing arguments to this getter

*/

keywordList = new Vector<String>();

try {

Database curDb = SystemUtils.getDb();

if (curDb.isOpen()) {

View kwLookupView = curDb.getView(getView());

// etc...

}

return keywordList;

}

}

Also investigate the concept of "managed properties": when registering a managed bean, you can also define what the initial values should be for one or more of the bean's properties. The values can be expression language, so they can be dynamically set when the bean instance is created.

Beans make the most sense to use in an XPage when they represent some business concept in your application that can be expressed as a series of properties. So the easiest way to start thinking in these terms is to look at the documents your app creates. Suppose you have a Contact form, with fields like firstName, lastName, emailAddress, etc. So now imagine that as a bean: private properties of firstName, lastName, emailAddress, and then public methods of getFirstName(), getLastName(), getEmailAddress(), and setters for each. That's a bean. And you would be able to bind XPage components to those properties (e.g. #{contact.firstName}), because the variable/property resolvers would know whether to call the getter or setter based on the current context.

Because we have a built-in data source for documents that allows us to use the same syntax to bind directly to fields on a document, it doesn't always make sense to treat document state as a bean. But once you go beyond documents, and start thinking about the people and processes surrounding document creation and modification in these terms, then you'll start to see the true advantages of using managed beans.

4Russell Maher  4/22/2013 12:18:41 PM  My Managed Bean Conundrum - Part 2

I was gonna' say what Tim said. Except not as good.

I will add that the detailed explanation in the post will ber VERY valuable to people.

Thanks for doing it.

5Steve Zavocki  4/22/2013 12:35:37 PM  My Managed Bean Conundrum - Part 2

Great description Tim,

I used to think as David said that the presence in the face-config determined whether it was considered managed or POJO.

To repeat back so I understand, what your saying Tim is that the bean spec must be followed exactly, and the way of determining whether it is managed is if you are able to use expression language to bind to it.

If it was a POJO, then you could still use its methods and it could be very useful, but you couldn't bind directly to it, like you can bind to a field or scoped variable.

Did I say that correctly?

6Bob Laing  4/22/2013 12:43:39 PM  My Managed Bean Conundrum - Part 2

Thanks Tim - I appreciate the feedback and code.

7Tim Tripcony  4/22/2013 1:08:25 PM  My Managed Bean Conundrum - Part 2

@Steve, beans are really just a convention; the term is used to describe a class that has private properties and public getters/setters that map to those properties. It doesn't stop being a bean if you decide to add other methods. But if the class doesn't have *any* properties encapsulated by getters/setters, it's not a bean.

You can still use EL binding if a bean instance is created in other ways: for instance, as the return object of a dataContext, or a member of a collection in a repeat control, and so forth. What makes a "managed bean" managed is that Domino creates the instance *for* you. You register the variable you want to use to refer to it, and then every time you refer to that variable, Domino checks to see whether it already has a valid instance of the bean in the scope you specified. If it does, it just returns it. Otherwise, it creates one, stores it in the correct scope, then returns it. That's the managed part: you don't have to create the object, and you don't have to store that... Domino manages that for you. You could do all that yourself:

function pseudoManagedBean() {

if (!viewScope.containsKey("myBean") {

viewScope.put("myBean", new com.acme.MyBean());

}

return viewScope.get("myBean");

}

var myBean = pseudoManagedBean();

So "managed" just means that you don't have to do all of the above to get a handle on myBean... the instant you refer to it, it exists, whether it already did or Domino had to create a new one for you. That's what a managed bean is managing.

8Steve Zavocki  4/22/2013 1:24:20 PM  My Managed Bean Conundrum - Part 2

Thanks Tim, that does clear it up for me. I appreciate your input!

9viagra_sale  9/19/2016 10:02:55 AM  viagra sale

{ Link } ,

10viagra_sale  9/19/2016 10:03:12 AM  viagra sale

{ Link } ,

11viagra_sale  9/19/2016 10:03:30 AM  viagra sale

{ Link } ,

12viagra_sale  9/19/2016 10:03:48 AM  viagra sale

I am in agreement with pretty much everything that you mentioned entirely! Excellent website document!

13via_gra_sale  9/19/2016 10:04:05 AM  via_gra sale

I am in agreement with pretty much everything that you mentioned entirely! Excellent website document!

14cialis  9/22/2016 8:01:06 AM  generic cialis overnigh

{ Link } , for daily use online, cialis best price online .

15cialis  9/22/2016 8:01:25 AM  generic cialis overnigh

Other medicines or treatments for ED How should you take TADALAFIL TROCHES ({ Link } , )?

16pharmacy  9/22/2016 1:17:50 PM  online pharmacy cialis

Fundamental minutes - The bunch we wonder in { Link } , dose for bph does not interfere useless anus.

17James  9/22/2016 1:18:18 PM  James

Appreciating the time and energy you put into your blog and in depth information you provide. It's good to come across a blog every once in a while that isn't the same unwanted rehashed information. Excellent read! I've saved your site and I'm adding your RSS feeds to my Google account.

18James  9/22/2016 1:18:40 PM  James

Appreciating the time and energy you put into your blog and in depth information you provide. It's good to come across a blog every once in a while that isn't the same unwanted rehashed information. Excellent read! I've saved your site and I'm adding your RSS feeds to my Google account.

19delivery  9/29/2016 8:53:08 AM  fast delivery viagra

{ Link } , For Sale In Usa Stores With Next Day Delivery in Corpus Christi .

20delivery  9/29/2016 8:53:27 AM  fast delivery viagra

{ Link } , dosage recommended, Natural fertility pills for twins!

21delivery  9/30/2016 8:37:38 AM  fast delivery viagra

How do I get free samples of { Link } , for my boyfriend?

22Daniel  9/30/2016 8:38:05 AM  Daniel

I truly appreciate this post. I've been looking everywhere for this! Thank goodness I found it on Bing. You've made my day! Thank you again!

23Daniel  9/30/2016 8:38:25 AM  Daniel

I truly appreciate this post. I've been looking everywhere for this! Thank goodness I found it on Bing. You've made my day! Thank you again!

24viagra  10/12/2016 6:30:20 AM  buy viagra online

A doctor will review your answers to make sure it�s safe for you to take { Link } , .

25viagra  10/12/2016 6:30:38 AM  buy viagra online

If you aren�t sure if it�s safe for you to take { Link } , , make an appointment with your doctor.

26viagra  10/15/2016 8:46:08 AM  buy viagra online

Tadalafil overnight delivery to Ketamine and { Link } , if we cover, then flag for work.

27Paul  10/15/2016 8:46:35 AM  Paul

Hello There. I found your blog using msn. This is a really well written article. Ill make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely return.

28Paul  10/15/2016 8:46:55 AM  Paul

Hello There. I found your blog using msn. This is a really well written article. Ill make sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely return.

29viagra_online  10/26/2016 4:50:49 AM  viagra online

{ Link } , For Women Herbal in Port Arthur .

30viagra_online  10/26/2016 4:51:08 AM  viagra online

An herbal alternative to { Link } , alternatives for both.

31viagra_online  10/27/2016 8:41:00 AM  viagra online

Buy Discounted Medicines natural { Link } , for women Online Without Prescription Best Prices.

32cialis_pills  11/17/2016 3:35:41 AM  cialis pills

Children buy generic { Link } , mastercard patients benefits very whether to for and says.

33cialis_pills  11/17/2016 3:35:58 AM  cialis pills

It works great for me so I have never thought giving { Link } , a try.

34cialis_pills  11/18/2016 9:12:01 AM  cialis pills

Taking { Link } , for daily use can increase the hardness of your erections.

35James  11/18/2016 9:12:26 AM  James

I've read some excellent stuff here. Definitely price bookmarking for revisiting. I surprise how much effort you place to create this type of great informative site.

36James  11/18/2016 9:12:45 AM  James

I've read some excellent stuff here. Definitely price bookmarking for revisiting. I surprise how much effort you place to create this type of great informative site.

37online  12/5/2016 9:59:30 AM  cialis online pharmacy

Goes a little wild generic { Link } , for women Ginger smell.

38online  12/5/2016 9:59:49 AM  cialis online pharmacy

With that, skin { Link } , for daily use reviews nice !

39from  12/6/2016 1:38:18 PM  cialis from india

Viagra for sale in the uk ::: Get free pills (viagra - { Link } , - levitra).

40Daniel  12/6/2016 1:38:44 PM  Daniel

I just couldn't depart your site before suggesting that I actually enjoyed the standard information a person provide for your visitors? Is gonna be back often in order to check up on new posts

41Daniel  12/6/2016 1:39:05 PM  Daniel

I just couldn't depart your site before suggesting that I actually enjoyed the standard information a person provide for your visitors? Is gonna be back often in order to check up on new posts

42cialis  12/28/2016 4:51:30 AM  discount cialis online

{ Link } , is not recommended for scheduled once daily use.

43cialis  12/28/2016 4:51:48 AM  discount cialis online

{ Link } , Affect, How viagra indicated for free!

44cialis  12/29/2016 5:55:36 AM  discount cialis online

Spe{ Link } , t Barrister for SDPA /police matters charges.

45cialis  2/9/2017 8:45:32 AM  http://c4generic.com/

WALGREEN PRICE FOR { Link } , s comments on cleveland.

46cialis  2/9/2017 8:45:50 AM  http://c4generic.com/

The { Link } , for sale vancouver had got into his fund and driven off; they followed him.

47cheap_cialis  2/9/2017 1:22:28 PM  cheap cialis

Viagra available bangalore eshop { Link } , sublingual for sale online!

48Paul  2/9/2017 1:22:52 PM  Paul

I like this post, enjoyed this one regards for putting up. The goal of revival is conformity to the image of Christ, not imitation of animals. by Richard F. Lovelace.

49Paul  2/9/2017 1:23:17 PM  Paul

I like this post, enjoyed this one regards for putting up. The goal of revival is conformity to the image of Christ, not imitation of animals. by Richard F. Lovelace.

50cialis  2/28/2017 4:42:17 AM  cheap cialis online

Gene transfer for by erectile efficacy h { Link } , cialis postdose in las vegas dr.

51cialis  2/28/2017 4:42:58 AM  cheap cialis online

{ Link } , a day � The next genre of treatment for erectile dysfunction .

52cialis  2/28/2017 6:21:23 AM  online cialis prescriptions

If you have an active sex life, { Link } , for Daily Use might be a good choice for you.

53cialis  2/28/2017 6:21:53 AM  online cialis prescriptions

I'm in a band generic { Link } , for less dose of polio vaccine for adults.

54cialis  3/1/2017 2:20:47 AM  cheap cialis online

See prices near you GoodRx Indications for { Link } , : Erectile dysfunction (ED).

55William  3/1/2017 2:21:15 AM  William

This is really attentiongrabbing, You're a very professional blogger. I have joined your rss feed and sit up for in search of extra of your fantastic post. Also, I have shared your site in my social networks!

56William  3/1/2017 2:21:42 AM  William

This is really attentiongrabbing, You're a very professional blogger. I have joined your rss feed and sit up for in search of extra of your fantastic post. Also, I have shared your site in my social networks!

57cialis  3/21/2017 1:44:50 PM  cialis

Ask for Price { Link } , Tablets Click to Zoom .

58cialis  3/21/2017 1:45:12 PM  cialis

When the personality provides for the pride s buy { Link } , cialis paulie knows barney fife .

59cialis  3/21/2017 6:14:58 PM  cialis

What the best price for { Link } , ?

60cialis  3/21/2017 6:16:00 PM  cialis

{ Link } , (tadalafil) is a prescription drug intended for efficient treatment of male impotence.

61prescription  3/23/2017 9:22:58 AM  us prescription cialis

Canadian pharmacy { Link } , Determining for the best option fall back ideal fat is crucial.

62George  3/23/2017 9:26:23 AM  George

Woah! I'm really loving the templatetheme of this website. It's simple, yet effective. A lot of times it's hard to get that perfect balance between usability and visual appeal. I must say you have done a great job with this. In addition, the blog loads super quick for me on Firefox. Excellent Blog!

63George  3/23/2017 9:35:41 AM  George

Woah! I'm really loving the templatetheme of this website. It's simple, yet effective. A lot of times it's hard to get that perfect balance between usability and visual appeal. I must say you have done a great job with this. In addition, the blog loads super quick for me on Firefox. Excellent Blog!