rs.close();
}
}
}
if (firstStructId < 0)
throw new PortalException("AggregatedUserLayoutStore::getAggregatedLayout(): No INIT_NODE_ID in UP_USER_LAYOUT_AGGR for " + userId + " and LAYOUT_ID " + layoutId);
}
} finally {
rs.close();
}
// we have to delete all the records from up_layout_struct_aggr table related to the lost nodes.
// do this only if we are not deferring to the template. (once in a great while 'we' will BE the template)
if (userId == realUserId){
/* end */
log.debug("deleting lost nodes because userId:"+userId+"== realUserId:"+realUserId);
stmt.executeUpdate("DELETE FROM UP_LAYOUT_STRUCT_AGGR WHERE USER_ID="+userId+" AND LAYOUT_ID="+layoutId+" AND PRNT_NODE_ID="+LOST_FOLDER_ID);
}else{
log.debug("not deleting lost nodes because userId:"+userId+"!= realUserId:"+realUserId);
}
// Instantiating the layout and setting the layout ID
layout = new AggregatedLayout ( layoutId + "" );
String restrLayoutSQL = "SELECT RESTRICTION_NAME, RESTRICTION_VALUE, RESTRICTION_TREE_PATH FROM UP_LAYOUT_RESTRICTIONS "+
"WHERE LAYOUT_ID="+layoutId+" AND USER_ID="+userId+" AND NODE_ID=?";
String restrFragmentSQL = "SELECT RESTRICTION_NAME, RESTRICTION_VALUE, RESTRICTION_TREE_PATH FROM UP_FRAGMENT_RESTRICTIONS "+
"WHERE FRAGMENT_ID=? AND NODE_ID=?";
// Creating a root folder
rootNode = ALFolder.createRootFolder();
// Setting the first layout node ID to the root folder
rootNode.setFirstChildNodeId(firstStructId+"");
// Putting the root node
layoutData.put(IALFolderDescription.ROOT_FOLDER_ID,rootNode);
// Putting the lost folder
layoutData.put(IALFolderDescription.LOST_FOLDER_ID,ALFolder.createLostFolder());
// layout query
String sqlLayout = "SELECT ULS.NODE_ID,ULS.NEXT_NODE_ID,ULS.CHLD_NODE_ID,ULS.PREV_NODE_ID,ULS.PRNT_NODE_ID,ULS.CHAN_ID,ULS.NAME,ULS.TYPE,ULS.HIDDEN,"+
"ULS.UNREMOVABLE,ULS.IMMUTABLE,ULS.PRIORITY,ULS.FRAGMENT_ID,ULS.FRAGMENT_NODE_ID";
if (ALuseOuterJoins) {
sqlLayout += ",USP.STRUCT_PARM_NM,USP.STRUCT_PARM_VAL FROM " + RDBMServices.getDbMetaData().getJoinQuery().getQuery("layout_aggr");
} else {
sqlLayout += " FROM UP_LAYOUT_STRUCT_AGGR ULS WHERE ";
}
sqlLayout += " ULS.USER_ID="+userId+" AND ULS.LAYOUT_ID="+layoutId;
log.debug(sqlLayout);
// The query for getting information of the fragments
String sqlFragment = "SELECT DISTINCT UF.NODE_ID,UF.NEXT_NODE_ID,UF.CHLD_NODE_ID,UF.PREV_NODE_ID,UF.PRNT_NODE_ID,UF.CHAN_ID,UF.NAME,UF.TYPE,UF.HIDDEN,"+
"UF.UNREMOVABLE,UF.IMMUTABLE,UF.PRIORITY,UF.FRAGMENT_ID ";
sqlFragment += "FROM UP_FRAGMENTS UF, UP_LAYOUT_STRUCT_AGGR ULS ";
sqlFragment += "WHERE ULS.USER_ID="+userId+" AND ULS.FRAGMENT_ID=UF.FRAGMENT_ID ";
sqlFragment += "UNION ";
sqlFragment += "SELECT DISTINCT UF.NODE_ID,UF.NEXT_NODE_ID,UF.CHLD_NODE_ID,UF.PREV_NODE_ID,UF.PRNT_NODE_ID,UF.CHAN_ID,UF.NAME,UF.TYPE,UF.HIDDEN,"+
"UF.UNREMOVABLE,UF.IMMUTABLE,UF.PRIORITY,UF.FRAGMENT_ID ";
sqlFragment += "FROM UP_FRAGMENTS UF ";
sqlFragment += ((pushFragmentIds!=null)?"WHERE UF.FRAGMENT_ID IN ("+pushFragmentIds+")":"");
// String sqlFragment = "SELECT DISTINCT UF.NODE_ID,UF.NEXT_NODE_ID,UF.CHLD_NODE_ID,UF.PREV_NODE_ID,UF.PRNT_NODE_ID,UF.CHAN_ID,UF.NAME,UF.TYPE,UF.HIDDEN,"+
// "UF.UNREMOVABLE,UF.IMMUTABLE,UF.PRIORITY,UF.FRAGMENT_ID";
// if (RDBMServices.supportsOuterJoins) {
// sqlFragment += ",UFP.PARAM_NAME,UFP.PARAM_VALUE FROM UP_LAYOUT_STRUCT_AGGR ULS, " + fragmentJoinQuery;
// } else {
// sqlFragment += " FROM UP_FRAGMENTS UF, UP_LAYOUT_STRUCT_AGGR ULS WHERE ";
// }
// sqlFragment += "(ULS.USER_ID="+userId+" AND ULS.FRAGMENT_ID=UF.FRAGMENT_ID)" + ((pushFragmentIds!=null)?" OR UF.FRAGMENT_ID IN ("+pushFragmentIds+")":"");
//
log.debug(sqlFragment);
// The hashtable object containing the fragment nodes that are next to the user layout nodes
Hashtable fragmentNodes = new Hashtable();
int count = 0;
for ( String sql = sqlLayout; count < 2; sql = sqlFragment, count++ ) {
List chanIds = Collections.synchronizedList(new ArrayList());
StringBuffer structParms = new StringBuffer();
rs = stmt.executeQuery(sql);
try {
int lastStructId = 0;
String sepChar = "";
if (rs.next()) {
int structId = rs.getInt(1);
/*if (rs.wasNull()) {
structId = 0;
}*/
readLayout: while (true) {
if (DEBUG > 1) System.err.println("Found layout structureID " + structId);
int nextId = rs.getInt(2);
/*if (rs.wasNull()) {
nextId = 0;
}*/
int childId = rs.getInt(3);
/*if (rs.wasNull()) {
childId = 0;
}*/
int prevId = rs.getInt(4);
/*if (rs.wasNull()) {
prevId = 0;
}*/
int prntId = rs.getInt(5);
/*if (rs.wasNull()) {
prntId = 0;
}*/
int chanId = rs.getInt(6);
/*if (rs.wasNull()) {
chanId = 0;
}*/
int fragmentId = rs.getInt(13);
int fragmentNodeId = ( sql.equals(sqlLayout) )?rs.getInt(14):0;
IALNodeDescription nodeDesc= null;
// Trying to get the node if it already exists
//ALNode node = (ALNode) layout.get(structId+"");
ALNode node;
String childIdStr = null;
//if ( ( chanId <= 0 && fragmentId <= 0 ) || ( fragmentId > 0 && ( childId > 0 || fragmentNodeId > 0 ) ) ) {
if ( chanId <= 0 ) {
//if ( node == null )
node = new ALFolder();
IALFolderDescription folderDesc = new ALFolderDescription();
// If children exist in the folder
if ( childId > 0 )
childIdStr = ( fragmentId > 0 && fragmentNodeId <= 0 )?(fragmentId+NODE_SEPARATOR+childId):(childId+"");
((ALFolder)node).setFirstChildNodeId(childIdStr);
String type = rs.getString(8);
int intType;
if ( "header".equalsIgnoreCase(type))
intType = UserLayoutFolderDescription.HEADER_TYPE;
else if ( "footer".equalsIgnoreCase(type))
intType = UserLayoutFolderDescription.FOOTER_TYPE;
else
intType = UserLayoutFolderDescription.REGULAR_TYPE;
folderDesc.setFolderType(intType);
nodeDesc = folderDesc;
} else {
//if ( node == null )
node = new ALChannel();
ALChannelDescription channelDesc = new ALChannelDescription();
channelDesc.setChannelPublishId(rs.getString(6));
nodeDesc = channelDesc;
}
// Setting node description attributes
if ( node.getNodeType() == IUserLayoutNodeDescription.FOLDER )
nodeDesc.setName(rs.getString(7));
nodeDesc.setHidden(("Y".equalsIgnoreCase(rs.getString(9))?true:false));
if ( fragmentId > 0 )
nodeDesc.setImmutable(true);
else
nodeDesc.setImmutable(("Y".equalsIgnoreCase(rs.getString(11))?true:false));
nodeDesc.setUnremovable(("Y".equalsIgnoreCase(rs.getString(10))?true:false));
node.setPriority(rs.getInt(12));
nodeDesc.setFragmentId((fragmentId>0)?fragmentId+"":null);
if ( sql.equals(sqlLayout) ) {
nodeDesc.setFragmentNodeId((fragmentNodeId>0)?fragmentNodeId+"":null);
}
// Setting the node id
if ( fragmentId > 0 && fragmentNodeId <= 0 )
nodeDesc.setId(fragmentId+NODE_SEPARATOR+structId);
else
nodeDesc.setId((structId!=LOST_FOLDER_ID)?(structId+""):IALFolderDescription.LOST_FOLDER_ID);
// Setting the next node id
if ( nextId != 0 ) {
//node.setNextNodeId((nextId!=LOST_NODE_ID)?(nextId+""):IALFolderDescription.LOST_FOLDER_ID);
String nextIdStr = ( fragmentId > 0 && fragmentNodeId <= 0 )?(fragmentId+NODE_SEPARATOR+nextId):(nextId+"");
node.setNextNodeId(nextIdStr);
}
String parentId;
switch ( prntId ) {
case 0:
parentId = IALFolderDescription.ROOT_FOLDER_ID;
break;
case LOST_FOLDER_ID:
parentId = IALFolderDescription.LOST_FOLDER_ID;
break;
default:
parentId = ( fragmentId > 0 && fragmentNodeId <= 0 )?(fragmentId+NODE_SEPARATOR+prntId):(prntId+"");
}
// Setting up the parent id
node.setParentNodeId(parentId);
// Setting the previous node id
if ( prevId != 0 ) {
//node.setPreviousNodeId((prevId!=LOST_NODE_ID)?(prevId+""):IALFolderDescription.LOST_FOLDER_ID);
String prevIdStr = ( fragmentId > 0 && fragmentNodeId <= 0 )?(fragmentId+NODE_SEPARATOR+prevId):(prevId+"");
node.setPreviousNodeId(prevIdStr);
}
lastStructId = structId;
String fragmentNodeIdStr = nodeDesc.getFragmentNodeId();
String fragmentIdStr = nodeDesc.getFragmentId();
String key = fragmentId+NODE_SEPARATOR+structId;
// Putting the node into the layout hashtable with an appropriate key
node.setNodeDescription(nodeDesc);
if ( fragmentNodeIdStr != null ) {
fragmentNodes.put(fragmentIdStr+NODE_SEPARATOR+fragmentNodeIdStr,node);
} else {
if ( fragmentIdStr != null && fragmentNodes.containsKey(key) ) {
ALNode fragNode = (ALNode) fragmentNodes.get(key);
//Keeping some properties of node description from the user layout for "pseudo" nodes
//IUserLayoutNodeDescription oldDesc = fragNode.getNodeDescription();
nodeDesc.setId(fragNode.getId());
nodeDesc.setFragmentNodeId(fragNode.getFragmentNodeId());
//nodeDesc.setName(oldDesc.getName());
nodeDesc.setImmutable(true);
fragNode.setNodeDescription(nodeDesc);
if ( fragNode.getNodeType() == IUserLayoutNodeDescription.FOLDER ) {
((ALFolder)fragNode).setFirstChildNodeId(childIdStr);
}
layoutData.put(nodeDesc.getId(),fragNode);
} else
layoutData.put(nodeDesc.getId(),node);
}
// If there is a channel we need to get its parameters
IALChannelDescription channelDesc = null;
if ( node.getNodeType() == IUserLayoutNodeDescription.CHANNEL ) {
channelDesc = (IALChannelDescription) nodeDesc;
chanIds.add(nodeDesc.getId());
}
// getting restrictions for the nodes
PreparedStatement psRestr = null;
if ( sql.equals(sqlLayout) && fragmentNodeId <= 0) {
psRestr = con.prepareStatement(restrLayoutSQL);
psRestr.setInt(1,structId);
} else {
psRestr = con.prepareStatement(restrFragmentSQL);
psRestr.setInt(1,fragmentId);
psRestr.setInt(2,(fragmentNodeId>0)?fragmentNodeId:structId);
}
ResultSet rsRestr = psRestr.executeQuery();
while (rsRestr.next()) {
String restrName = rsRestr.getString(1);
String restrExp = rsRestr.getString(2);
String restrPath = rsRestr.getString(3);
if ( restrPath == null || restrPath.trim().length() == 0 )
restrPath = IUserLayoutRestriction.LOCAL_RESTRICTION_PATH;
IUserLayoutRestriction restriction = UserLayoutRestrictionFactory.createRestriction(restrName,restrExp,restrPath);
nodeDesc.addRestriction(restriction);
}
rsRestr.close();
if ( psRestr != null ) psRestr.close();
int index = (sql.equals(sqlLayout))?15:14;
if (ALuseOuterJoins) {
do {
String name = rs.getString(index);
String value = rs.getString(index+1); // Oracle JDBC requires us to do this for longs
if (name != null) { // may not be there because of the join
if ( channelDesc != null )
channelDesc.setParameterValue(name,value);
}
if (!rs.next()) {
break readLayout;
}
structId = rs.getInt(1);
if (rs.wasNull()) {
structId = 0;
}
} while (structId == lastStructId);
} else { // Do second SELECT later on for structure parameters
// Adding the channel ID to the String buffer
if ( node.getNodeType() == IUserLayoutNodeDescription.CHANNEL ) {
structParms.append(sepChar + chanId);
sepChar = ",";
}
if (rs.next()) {
structId = rs.getInt(1);
if (rs.wasNull()) {
structId = 0;
}
} else {
break readLayout;
}
} //end else
// Setting up the priority values based on the appropriate priority restrictions
PriorityRestriction priorityRestriction = AggregatedLayoutManager.getPriorityRestriction(node);
if ( priorityRestriction != null ) {
int priority = node.getPriority();
int[] range = priorityRestriction.getRange();
int newPriority = priority;
if ( range[0] > priority )
newPriority = range[0];
else if ( range[1] < priority )
newPriority = range[1];
// Changing the node priority if it's been changed
if ( newPriority != priority )
node.setPriority(newPriority);
}
} // while
/*
if ( psRestrLayout != null ) psRestrLayout.close();
if ( psRestrFragment != null ) psRestrFragment.close();
*/
}
} finally {
rs.close();
}
// We have to retrieve the channel defition after the layout structure
// since retrieving the channel data from the DB may interfere with the
// layout structure ResultSet (in other words, Oracle is a pain to program for)
if (chanIds.size() > 0) {
for (int i = 0; i < chanIds.size(); i++) {
String key = (String) chanIds.get(i);
ALNode node = (ALNode) layoutData.get(key);
fillChannelDescription( (IALChannelDescription) node.getNodeDescription() );
}
chanIds.clear();
}
if ( !ALuseOuterJoins && structParms.length() > 0 ) { // Pick up structure parameters
String paramSql = "SELECT STRUCT_ID, STRUCT_PARM_NM,STRUCT_PARM_VAL FROM UP_LAYOUT_PARAM WHERE USER_ID=" + userId + " AND LAYOUT_ID=" + layoutId +
" AND STRUCT_ID IN (" + structParms.toString() + ") ORDER BY STRUCT_ID";
if (log.isDebugEnabled())
log.debug("AggregatedUserLayoutStore::getUserLayout(): " + paramSql);
// Adding this to prevent the error "closed statement" in Oracle
Statement st = con.createStatement();
rs = st.executeQuery(paramSql);
try {
if (rs.next()) {
int structId = rs.getInt(1);
readParm: while(true) {
//LayoutStructure ls = (LayoutStructure)layoutStructure.get(new Integer(structId));
ALNode node = (ALNode) layoutData.get(structId+"");
if ( node != null ) {
IALChannelDescription channelDesc = (IALChannelDescription) node.getNodeDescription();
int lastStructId = structId;
do {
//ls.addParameter(rs.getString(2), rs.getString(3));
String name = rs.getString(2);
String value = rs.getString(3);
channelDesc.setParameterValue(name,value);
if (!rs.next()) {
break readParm;
}
} while ((structId = rs.getInt(1)) == lastStructId);
} else break readParm; // if else
}
}
} finally {
rs.close();
st.close();
}
}
} // End of for
// Very suspicious place !!!!
// Check if the node from an user layout points to a fragment node, we have to bind them
// The loop for all the nodes from the hashtable
//System.out.println( "Getting layout size: " + layout.size() );
/*for ( Enumeration nodeIds = layout.keys(); nodeIds.hasMoreElements() ;) {
String strNodeId = nodeIds.nextElement().toString();
//System.out.println( "Getting nodeId: " + strNodeId );
ALNode node = (ALNode) layout.get(strNodeId);
if ( node.getFragmentId() == null ) {
String nextNodeId = node.getNextNodeId();
ALNode nextNode = null;
if ( nextNodeId != null ) nextNode = (ALNode) layout.get(nextNodeId);
String prevNodeId = node.getPreviousNodeId();
ALNode prevNode = null;
if ( prevNodeId != null ) prevNode = (ALNode) layout.get(prevNodeId);
String prntNodeId = node.getParentNodeId();
ALNode prntNode = null;
if ( prntNodeId != null ) prntNode = (ALNode) layout.get(prntNodeId);
String firstChildId = ("folder".equals(node.getNodeType()))?((ALFolder)node).getFirstChildNodeId():null;
ALNode childNode = null;
if ( firstChildId != null )
childNode = (ALNode)layout.get(firstChildId);
if ( nextNode != null && nextNode.getFragmentId() != null ) nextNode.setPreviousNodeId(strNodeId);
if ( prevNode != null && prevNode.getFragmentId() != null ) prevNode.setNextNodeId(strNodeId);
// if we have the following: node = the first child of his parent what is a fragment node :))
if ( prntNode != null && prntNode.getFragmentId() != null && node.getPreviousNodeId() == null )
if ( "folder".equals(prntNode.getNodeType()) )
((ALFolder)prntNode).setFirstChildNodeId(strNodeId);
// Checking all the children
if ( childNode != null ) {
for ( String tmpNodeId = childNode.getId(); tmpNodeId != null; ) {
ALNode tmpChildNode = (ALNode) layout.get(tmpNodeId);
// if we got tmpChildNode == NULL we have to get out of the loop
//if ( tmpChildNode != null ) {
if ( tmpChildNode.getFragmentId() != null )
tmpChildNode.setParentNodeId(strNodeId);
tmpNodeId = tmpChildNode.getNextNodeId();
//} else break;
}
}
}
}
*/
// finding the last node in the sibling line of the root children
ALNode lastNode = null, prevNode = null;
String nextId = rootNode.getFirstChildNodeId();
while ( nextId != null ) {
lastNode = (ALNode)layoutData.get(nextId);
// If neccessary cleaning the end of tabs sibling line setting the next ID to null of the last tab
if ( lastNode == null ) {
if ( prevNode != null ) {
prevNode.setNextNodeId(null);
lastNode = prevNode;
}
break;
}
nextId = lastNode.getNextNodeId();
prevNode = lastNode;
}
// Binding the push-fragments to the end of the sibling line of the root children
if ( pushFragmentRoots != null ) {
for ( Enumeration fragmentIds = pushFragmentRoots.keys(); fragmentIds.hasMoreElements() ;) {
String strFragmentId = fragmentIds.nextElement().toString();
String strFragmentRootId = pushFragmentRoots.get(strFragmentId).toString();
String key = strFragmentId+NODE_SEPARATOR+strFragmentRootId;
ALNode node = (ALNode) layoutData.get(key);
if ( node != null ) {
IALNodeDescription nodeDesc = (IALNodeDescription) node.getNodeDescription();
// Setting the new next struct node ID and fragment node id since we have all the pushed fragments attached to the layout
String newId = getNextStructId(person,"");
nodeDesc.setId(newId);
nodeDesc.setFragmentNodeId(strFragmentRootId);
// Remove the old node and put the new one with another ID
layoutData.remove(key);
layoutData.put(newId,node);
if ( lastNode != null ) {
lastNode.setNextNodeId(newId);
node.setPreviousNodeId(lastNode.getId());
} else
rootNode.setFirstChildNodeId(newId);
if ( node.getNodeType() == IUserLayoutNodeDescription.FOLDER ) {
//Changing the parent Ids for all the children
for ( String nextIdStr = ((ALFolder)node).getFirstChildNodeId(); nextIdStr != null; ) {
ALNode child = (ALNode) layoutData.get(nextIdStr);
child.setParentNodeId(newId);
nextIdStr = child.getNextNodeId();
}
}
node.setParentNodeId(IALFolderDescription.ROOT_FOLDER_ID);
lastNode = node;
}
} // end for
} // end if
for ( Enumeration fragmentNodesEnum = fragmentNodes.keys(); fragmentNodesEnum.hasMoreElements() ;) {
String key = fragmentNodesEnum.nextElement().toString();
ALNode node = (ALNode ) fragmentNodes.get(key);
if ( node.getNodeType() == IUserLayoutNodeDescription.FOLDER ) {
String parentId = node.getId();
for ( String nextIdStr = ((ALFolder)node).getFirstChildNodeId(); nextIdStr != null; ) {
ALNode child = (ALNode) layoutData.get(nextIdStr);
child.setParentNodeId(parentId);
nextIdStr = child.getNextNodeId();
}
}
}
if (log.isDebugEnabled()){
long stopTime = System.currentTimeMillis();
log.debug("AggregatedUserLayoutStore::getUserLayout(): " +
"Layout document for user " + userId + " took " +
(stopTime - startTime) + " milliseconds to create");
}
} finally {
if ( insertStmt != null ) insertStmt.close();
if ( stmt != null ) stmt.close();
}
} catch ( Exception e ) {
log.error("Error getting aggregated layout for user " + person, e);
throw new PortalException(e);
} finally {
RDBMServices.releaseConnection(con);
}
layout.setLayoutData(layoutData);