留言板

How to rebuild assetCategory tree

François LE QUEMENER,修改在12 年前。

How to rebuild assetCategory tree

Junior Member 帖子: 48 加入日期: 09-9-18 最近的帖子
Hi,
I'm adding categories through the AssetCategoryLocalServiceUtil.addCategory method. I found out that the categories created have wrong "leftCategoryId" and "rightCategoryId"

I guess I must rebuild the assetCategory tree, I found that method:
AssetCategoryUtil.rebuildTree(groupId, true);


When executing this method, I get this Exception:
 org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
	at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:685)
	at com.liferay.portal.dao.orm.hibernate.SessionFactoryImpl.openSession(SessionFactoryImpl.java:58)
	at com.liferay.portal.service.persistence.impl.BasePersistenceImpl.openSession(BasePersistenceImpl.java:178)
	at com.liferay.portlet.asset.service.persistence.AssetCategoryPersistenceImpl.rebuildTree(AssetCategoryPersistenceImpl.java:5092)
	... 28 more


I guess I cannot use directly this method? I found nothing in AssetCategoryLocalServiceUtil to rebuild this tree.

Thanks
thumbnail
Nicolas Grué,修改在12 年前。

RE: How to rebuild assetCategory tree

New Member 帖子: 13 加入日期: 07-2-20 最近的帖子
Hi François,

We stumble upon the same issue. Reason is in former versions of Liferay portal 6.0, there was a glitch in method setParentCategoryId (inside AssetCategoryModelImpl.java)
It has been corrected since.

But like you, we are unable to cleanly rebuild our old buggy categories tree.

I'll let you know if we find something, but in the meantime if you found a solution, please share it: I suppose it would be useful to many !
thumbnail
Nicolas Grué,修改在12 年前。

RE: How to rebuild assetCategory tree

New Member 帖子: 13 加入日期: 07-2-20 最近的帖子
Finally we'll have to rewrite the same algorithm without transaction management.

Here is the code, for your info:

	/**
	 * As it says: rebuilds all categories trees for Group groupId.
	 * @param messageBuffer
	 * @param groupId
	 * @throws SystemException
	 * @throws PortalException
	 */
	public void rebuildTree(StringBuffer messageBuffer, long groupId) throws SystemException, PortalException {
		rebuildTree(messageBuffer, groupId, 0, 1);

	}

	/**
	 * As it says: rebuilds tree for category parentCategoryId.
	 * @param messageBuffer
	 * @param groupId
	 * @param parentCategoryId
	 * @param leftCategoryId
	 * @return
	 * @throws SystemException
	 * @throws PortalException
	 */
	@SuppressWarnings("unchecked")
	protected long rebuildTree(StringBuffer messageBuffer, long groupId, long parentCategoryId,
			long leftCategoryId) throws SystemException, PortalException {

		DynamicQuery query = DynamicQueryFactoryUtil.forClass(AssetCategory.class);
		query.add(PropertyFactoryUtil.forName("groupId").eq(groupId));
		query.add(PropertyFactoryUtil.forName("parentCategoryId").eq(parentCategoryId));
		query.addOrder(OrderFactoryUtil.asc("categoryId"));
		List<assetcategory> categs = AssetCategoryLocalServiceUtil.dynamicQuery(query);
		long rightCategoryId = leftCategoryId + 1;
		
		for (AssetCategory categ: categs) {
			rightCategoryId = rebuildTree(messageBuffer, groupId, categ.getCategoryId(), rightCategoryId);
		}

		if (parentCategoryId &gt; 0) {
			AssetCategory categ = AssetCategoryLocalServiceUtil.getAssetCategory(parentCategoryId);
			categ.setLeftCategoryId(leftCategoryId);
			categ.setRightCategoryId(rightCategoryId);
			AssetCategoryLocalServiceUtil.updateAssetCategory(categ);
			if (messageBuffer!=null) {
				messageBuffer.append(categ.getCategoryId()).append(';')
						.append(categ.getName()).append(';')
						.append(categ.getLeftCategoryId()).append(';')
						.append(categ.getRightCategoryId()).append('\n');
			}
		}
		return rightCategoryId + 1;
	}
</assetcategory>
François LE QUEMENER,修改在12 年前。

RE: How to rebuild assetCategory tree

Junior Member 帖子: 48 加入日期: 09-9-18 最近的帖子
We finally managed to do this by upgrading to Liferay 6.0.12 EE. The SP2 corrects this anomaly and gives the method rebuiltTree to rebuilt category tree.