throw new SystemException(e.getMessage());
}
db = CastorDatabaseService.getDatabase();
ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
beginTransaction(db);
try
{
if(beforeSiteNodeId == null && direction == null)
{
logger.warn("You must specify the either new location with beforeSiteNodeId or sortDirection");
throw new ConstraintException("SiteNode.parentSiteNodeId", "3403"); //TODO
}
//logger.info("siteNodeId:" + siteNodeId);
//logger.info("beforeSiteNodeId:" + beforeSiteNodeId);
//logger.info("direction:" + direction);
if(beforeSiteNodeId != null)
{
Integer oldSortOrder = 0;
Integer newSortOrder = 0;
SiteNodeVO siteNodeVO = getSiteNodeVOWithId(siteNodeId, db);
SiteNodeVO beforeSiteNodeVO = getSiteNodeVOWithId(beforeSiteNodeId, db);
if(beforeSiteNodeVO.getParentSiteNodeId().intValue() != siteNodeVO.getParentSiteNodeId().intValue())
{
logger.info("Was new parent - let's fix that as well");
}
else
{
logger.info("Parent was the same...");
List<SiteNodeVO> newChildSiteNodeList = new ArrayList<SiteNodeVO>();
int insertPosition = 0;
List<SiteNodeVO> childrenVOList = SiteNodeController.getController().getChildSiteNodeVOList(siteNodeVO.getParentSiteNodeId(), false, db, sortLanguageId);
Iterator<SiteNodeVO> childrenVOListIterator = childrenVOList.iterator();
while(childrenVOListIterator.hasNext())
{
SiteNodeVO childSiteNodeVO = childrenVOListIterator.next();
if(childSiteNodeVO.getSiteNodeId().equals(beforeSiteNodeId))
{
insertPosition = newChildSiteNodeList.size();
}
if(!childSiteNodeVO.getSiteNodeId().equals(siteNodeId))
{
newChildSiteNodeList.add(childSiteNodeVO);
}
}
newChildSiteNodeList.add(insertPosition, siteNodeVO);
Iterator<SiteNodeVO> newChildSiteNodeListIterator = newChildSiteNodeList.iterator();
int i=0;
int highestSortOrder = 0;
while(newChildSiteNodeListIterator.hasNext())
{
logger.info("i:" + i);
logger.info("highestSortOrder:" + highestSortOrder);
SiteNodeVO orderedSiteNodeVO = newChildSiteNodeListIterator.next();
List events = new ArrayList();
SiteNodeVersionVO siteNodeVersionVO = SiteNodeVersionController.getController().getLatestSiteNodeVersionVO(db, orderedSiteNodeVO.getId());
Integer localizedSortOrder = 100;
if(CmsPropertyHandler.getAllowLocalizedSortAndVisibilityProperties())
{
ContentVersionVO cvVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(orderedSiteNodeVO.getMetaInfoContentId(), sortLanguageId, db);
if(cvVO != null)
{
String localizedSortOrderString = ContentVersionController.getContentVersionController().getAttributeValue(cvVO.getId(), "SortOrder", false);
if(localizedSortOrderString != null && !localizedSortOrderString.equals(""))
localizedSortOrder = new Integer(localizedSortOrderString);
}
}
//if(siteNodeVersionVO.getSortOrder() < highestSortOrder || localizedSortOrder < highestSortOrder)
if(siteNodeVersionVO.getSortOrder() < highestSortOrder || localizedSortOrder < highestSortOrder || i != localizedSortOrder)
{
if(!siteNodeVersionVO.getStateId().equals(SiteNodeVersionVO.WORKING_STATE))
{
siteNodeVersionVO = SiteNodeStateController.getController().changeState(siteNodeVersionVO.getId(), SiteNodeVersionVO.WORKING_STATE, "Changed sortOrder", false, infoGluePrincipal, siteNodeVersionVO.getSiteNodeId(), events);
}
siteNodeVersionVO.setSortOrder(i);
if(CmsPropertyHandler.getAllowLocalizedSortAndVisibilityProperties())
{
ContentVersionVO cvVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(orderedSiteNodeVO.getMetaInfoContentId(), sortLanguageId, db);
if(cvVO != null)
ContentVersionController.getContentVersionController().updateAttributeValue(cvVO.getContentVersionId(), "SortOrder", "" + i, infoGluePrincipal, db, true);
}
SiteNodeVersionController.getController().update(siteNodeVersionVO);
}
else
logger.info("No action - sort order was ok");
/*
if(CmsPropertyHandler.getAllowLocalizedSortAndVisibilityProperties())
{
if(localizedSortOrder < 100 && localizedSortOrder > highestSortOrder)
highestSortOrder = localizedSortOrder;
if(highestSortOrder > i)
i = highestSortOrder;
}
else
{
highestSortOrder = siteNodeVersionVO.getSortOrder();
if(highestSortOrder > i)
i = highestSortOrder;
}
*/
i++;
}
}
}
else if(direction.equalsIgnoreCase("up") || direction.equalsIgnoreCase("down"))
{
Integer oldSortOrder = 0;
Integer newSortOrder = 0;
SiteNodeVO siteNodeVO = getSiteNodeVOWithId(siteNodeId, db);
SiteNodeVersion latestSiteNodeVersion = SiteNodeVersionController.getController().getLatestActiveSiteNodeVersion(db, siteNodeId);
if(latestSiteNodeVersion != null)
{
oldSortOrder = latestSiteNodeVersion.getSortOrder();
if(direction.equalsIgnoreCase("up"))
newSortOrder = oldSortOrder - 1;
else if(direction.equalsIgnoreCase("down"))
newSortOrder = oldSortOrder + 1;
}
logger.info("OldSortOrder:" + oldSortOrder);
List<SiteNodeVO> childrenVOList = SiteNodeController.getController().getChildSiteNodeVOList(siteNodeVO.getParentSiteNodeId(), false, db, sortLanguageId);
Iterator<SiteNodeVO> childrenVOListIterator = childrenVOList.iterator();
while(childrenVOListIterator.hasNext())
{
SiteNodeVO childSiteNodeVO = childrenVOListIterator.next();
logger.info("childSiteNodeVO:" + childSiteNodeVO.getId() + ":" + childSiteNodeVO.getSortOrder());
SiteNodeVersion latestChildSiteNodeVersion = SiteNodeVersionController.getController().getLatestActiveSiteNodeVersion(db, childSiteNodeVO.getId());
//logger.info("latestChildSiteNodeVersion:" + latestChildSiteNodeVersion.getId());
Integer currentSortOrder = latestChildSiteNodeVersion.getSortOrder();
logger.info("currentSortOrder:" + currentSortOrder);
if(currentSortOrder.equals(oldSortOrder))
{
latestChildSiteNodeVersion = SiteNodeStateController.getController().updateStateId(latestChildSiteNodeVersion, SiteNodeVersionVO.WORKING_STATE, "Changed sortOrder", infoGluePrincipal, db);
latestChildSiteNodeVersion.setSortOrder(newSortOrder);
//logger.info("Changed sort order on:" + latestChildSiteNodeVersion.getId() + " into " + newSortOrder);
if(CmsPropertyHandler.getAllowLocalizedSortAndVisibilityProperties())
{
ContentVersionVO cvVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(childSiteNodeVO.getMetaInfoContentId(), sortLanguageId, db);
if(cvVO != null)
ContentVersionController.getContentVersionController().updateAttributeValue(cvVO.getContentVersionId(), "SortOrder", "" + newSortOrder, infoGluePrincipal, db, true);
}
}
else if(currentSortOrder.equals(newSortOrder))
{
latestChildSiteNodeVersion = SiteNodeStateController.getController().updateStateId(latestChildSiteNodeVersion, SiteNodeVersionVO.WORKING_STATE, "Changed sortOrder", infoGluePrincipal, db);
latestChildSiteNodeVersion.setSortOrder(oldSortOrder);
//logger.info("Changed sort order on:" + latestChildSiteNodeVersion.getId() + " into " + oldSortOrder);
if(CmsPropertyHandler.getAllowLocalizedSortAndVisibilityProperties())
{
ContentVersionVO cvVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(childSiteNodeVO.getMetaInfoContentId(), sortLanguageId, db);
if(cvVO != null)
ContentVersionController.getContentVersionController().updateAttributeValue(cvVO.getContentVersionId(), "SortOrder", "" + oldSortOrder, infoGluePrincipal, db, true);
}
}
}
}
//If any of the validations or setMethods reported an error, we throw them up now before create.
ceb.throwIfNotEmpty();
commitTransaction(db);
}
catch(Exception e)
{