Skip to content

Instantly share code, notes, and snippets.

@perlausten
Created November 5, 2013 12:52
Show Gist options
  • Save perlausten/7318568 to your computer and use it in GitHub Desktop.
Save perlausten/7318568 to your computer and use it in GitHub Desktop.
package com.timtripcony.util;
/*
<!--
Copyright 2010 Tim Tripcony
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License
-->
*/
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.el.VariableResolver;
import lotus.domino.Database;
import lotus.domino.DateTime;
import lotus.domino.NotesException;
import lotus.domino.Session;
import com.ibm.xsp.application.DesignerApplicationEx;
import com.ibm.xsp.component.UIViewRootEx;
import com.ibm.xsp.designer.context.XSPContext;
public class JSFUtil {
private static Session _signerSess;
public static DesignerApplicationEx getApplication() {
return (DesignerApplicationEx) getFacesContext().getApplication();
}
@SuppressWarnings("unchecked")
public static Map<String, Object> getApplicationScope() {
return getServletContext().getApplicationMap();
}
@SuppressWarnings("unchecked")
public static Map<String, Object> getCompositeData() {
return (Map<String, Object>) getVariableResolver().resolveVariable(getFacesContext(), "compositeData");
}
public static Database getCurrentDatabase() {
return (Database) getVariableResolver().resolveVariable(getFacesContext(), "database");
}
public static Session getCurrentSession() {
return (Session) getVariableResolver().resolveVariable(getFacesContext(), "session");
}
public static FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
@SuppressWarnings("unchecked")
public static Map<String, Object> getRequestScope() {
return getServletContext().getRequestMap();
}
public static ExternalContext getServletContext() {
return getFacesContext().getExternalContext();
}
@SuppressWarnings("unchecked")
public static Map<String, Object> getSessionScope() {
return getServletContext().getSessionMap();
}
private static VariableResolver getVariableResolver() {
return getApplication().getVariableResolver();
}
public static UIViewRootEx getViewRoot() {
return (UIViewRootEx) getFacesContext().getViewRoot();
}
@SuppressWarnings("unchecked")
public static Map<String, Object> getViewScope() {
return getViewRoot().getViewMap();
}
public static XSPContext getXSPContext() {
return XSPContext.getXSPContext(getFacesContext());
}
public static Object resolveVariable(String variable) {
return FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), variable);
}
@SuppressWarnings("unchecked")
public static String getProfileUNID(String profileKey) {
try {
Vector profHash = getCurrentSession().evaluate("@Text(@Password(\"" + profileKey + "\"))");
String tmpUNID = profHash.firstElement().toString();
tmpUNID = tmpUNID.substring(1, tmpUNID.length() - 1);
return tmpUNID;
} catch (NotesException e) {
return "";
}
}
public static Session getSession() {
return (Session) resolveVariable("session");
}
public static Session getSessionAsSigner() {
return (Session) resolveVariable("sessionAsSigner");
}
public static Session getSessionAsSignerWithFullAccess() {
return (Session) resolveVariable("sessionAsSignerWithFullAccess");
}
// returns a handle to the current database using a sessionAsSigner
public static Database getCurrentDatabaseAsSigner() throws NotesException {
Database db = JSFUtil.getCurrentDatabase();
return getSessionAsSigner().getDatabase(db.getServer(), db.getFilePath());
}
public static Date toJavaDateSafe(DateTime dt) {
Date date = null;
if (dt != null) {
try {
date = dt.toJavaDate();
} catch (NotesException ne) {
// do nothing
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
dt.recycle();
} catch (NotesException nex) {
}
}
}
return date;
}
/**
* Finds an UIComponent by its component identifier in the current component tree.
*
* @param compId
* the component identifier to search for
* @return found UIComponent or null
*
* @throws NullPointerException
* if <code>compId</code> is null
*/
public static UIComponent findComponent(String compId) {
return findComponent(FacesContext.getCurrentInstance().getViewRoot(), compId);
}
/**
* Finds an UIComponent by its component identifier in the component tree below the specified <code>topComponent</code> top component.
*
* @param topComponent
* first component to be checked
* @param compId
* the component identifier to search for
* @return found UIComponent or null
*
* @throws NullPointerException
* if <code>compId</code> is null
*/
public static UIComponent findComponent(UIComponent topComponent, String compId) {
if (compId == null)
throw new NullPointerException("Component identifier cannot be null");
if (compId.equals(topComponent.getId()))
return topComponent;
if (topComponent.getChildCount() > 0) {
List<UIComponent> childComponents = topComponent.getChildren();
for (UIComponent currChildComponent : childComponents) {
UIComponent foundComponent = findComponent(currChildComponent, compId);
if (foundComponent != null)
return foundComponent;
}
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment