75. Some useful methods for Java Development in XPages

Source: Internet
Author: User
Tags addall

When using Java for XPages development, there are some common basic tasks. These common tasks are the same as those encountered during the development of the Lotus Notes client, such as obtaining the current Session and database object, but the method achieved is completely different from that achieved by using Lotus script; other requirements are specific to the XPages development environment, such as obtaining the current com. ibm. xsp. designer. context. XSPContext and RequestMap objects (that is, the RequestScope variable ). It is useful to write these frequently needed tasks in the form of static methods in a tool class:

package starrow.xsp;import java.lang.reflect.Method;import java.util.*;import javax.faces.context.FacesContext;import starrow.AppException;import lotus.domino.*;import lotus.domino.local.NotesBase;import com.acme.tools.JSFUtil;import com.ibm.designer.runtime.directory.DirectoryUser;import com.ibm.xsp.component.UIViewRootEx2;import com.ibm.xsp.designer.context.XSPContext;import com.ibm.xsp.model.DataSource;import com.ibm.xsp.model.domino.DominoDocumentData;import com.ibm.xsp.model.domino.DominoUtils;import com.ibm.xsp.model.domino.wrapped.DominoDocument;@SuppressWarnings("unchecked")public class XSPUtil {public static Session getSession(){//return (Session) JSFUtil.getVariableValue("session");return DominoUtils.getCurrentSession();}public static Session getSessionAsSigner(){return (Session) JSFUtil.getVariableValue("sessionAsSigner");}public static Database getDatabase() throws NotesException{return getSession().getCurrentDatabase();//return (Database) JSFUtil.getVariableValue("database");}public static Database getDatabase(String dbPath) throws NotesException{String server=getDatabase().getServer();return getDatabase(server, dbPath);}public static Database getDatabase(String server, String dbPath) throws NotesException{return getSession().getDatabase(server, dbPath);}public static XSPContext getContext(){//return (XSPContext) JSFUtil.getVariableValue("context");FacesContext fc=FacesContext.getCurrentInstance();return XSPContext.getXSPContext(fc);}public static Document getCurrentDocument() throws Exception{UIViewRootEx2 view=(UIViewRootEx2) FacesContext.getCurrentInstance().getViewRoot();for (DataSource ds : view.getData()){if (ds instanceof DominoDocumentData){DominoDocumentData ddd=(DominoDocumentData) ds;DominoDocument dd=(DominoDocument) ddd.getDataObject();return dd.getDocument();}}throw new AppException("No document data source is found.");//return null;}public static DominoDocument getCurrentDominoDocument() throws Exception{UIViewRootEx2 view=(UIViewRootEx2) FacesContext.getCurrentInstance().getViewRoot();for (DataSource ds : view.getData()){if (ds instanceof DominoDocumentData){DominoDocumentData ddd=(DominoDocumentData) ds;return (DominoDocument) ddd.getDataObject();}}throw new AppException("No document data source is found.");//return null;}public static Object[] getRoles(){XSPContext context=XSPUtil.getContext();List userRoles=context.getUser().getRoles();return userRoles.toArray();}public static boolean hasRoles(String[] roles){XSPContext context=XSPUtil.getContext();List userRoles=context.getUser().getRoles();for (Object ur : userRoles){for (String r : roles){if (ur.toString().equals(r)){return true;}}}return false;}public static boolean hasRole(String role){String[] roles={role};return hasRoles(roles);}//returns a Vector
 
   containing the name, groups and roles of the current user.public static Vector
  
    getUserNamesList() throws NotesException{Vector
   
     result=new Vector
    
     ();DirectoryUser user=getContext().getUser();//DirectoryUser.getFullName() returns the common name.//I can reproduce the issue in both 8.5.1-3.//For local preview, getFullName() returns Anonymous, getDistinguishedName() returns anonymous.result.add(getSession().getEffectiveUserName());result.addAll(user.getGroups());result.addAll(user.getRoles());return result;}public static Map getRequestMap(){return FacesContext.getCurrentInstance().getExternalContext().getRequestMap();}public static void feedback(String msg){getRequestMap().put("message", msg);}}
    
   
  
 

A little description of this tool class:

Feedback () transmits a string message to RequestMap and stores it with the message key for display by the message control on the page. The agreed key and method can be used by the managed bean to feedback messages to the front-end.

The getContext () method returns the com. ibm. xsp. designer. context. XSPContext object, which is used as an extension of FacesContext to provide context information for XPage runtime.

The getCurrentDocument () and getCurrentDominoDocument () Methods return the current Document and DominoDocument objects respectively.

The three overload methods of getDatabase () return the current database object based on the parameters respectively.

The getRequestMap () method returns the java. util. Map object representing the RequestScope variable.

GetRoles () returns all roles of the current user.

GetSession () and SessionAsSigner () return the current Session and the Session object signed by the program respectively.

GetUserNamesList () imitates the @ UserNamesList function and returns the Vector Contains all user names, roles, and groups of the current user.

HasRole () and hasRoles () Determine whether the current user has either of the given roles.

The AppException used in the program is an extension of the simple Exception mentioned previously, used to create custom exceptions.

package starrow;public class AppException extends Exception {private static final long serialVersionUID = -143353750902427769L;public AppException(String message){super(message);}}

Com. acme. tools. JSFUtil from KarstenLehmann's blog: http://www.mindoo.de/web/blog.nsf/dx/18.07.2009191738KLENAL.htm? Opendocument




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.