001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.model.TreeModel;
019    
020    import java.util.ArrayList;
021    import java.util.Deque;
022    import java.util.LinkedList;
023    import java.util.List;
024    
025    /**
026     * @author Shinn Lok
027     */
028    public class TreePathUtil {
029    
030            public static void rebuildTree(
031                            long companyId, long parentPrimaryKey, String parentTreePath,
032                            TreeModelTasks<?> treeModelTasks)
033                    throws PortalException {
034    
035                    List<TreeModel> modifiedTreeModels = new ArrayList<>();
036    
037                    Deque<Object[]> traces = new LinkedList<>();
038    
039                    traces.push(new Object[] {parentPrimaryKey, parentTreePath, 0L});
040    
041                    Object[] trace = null;
042    
043                    while ((trace = traces.poll()) != null) {
044                            Long curParentPrimaryKey = (Long)trace[0];
045                            String curParentTreePath = (String)trace[1];
046                            Long previousPrimaryKey = (Long)trace[2];
047    
048                            treeModelTasks.rebuildDependentModelsTreePaths(
049                                    curParentPrimaryKey, curParentTreePath);
050    
051                            List<? extends TreeModel> treeModels =
052                                    treeModelTasks.findTreeModels(
053                                            previousPrimaryKey, companyId, curParentPrimaryKey,
054                                            _MODEL_TREE_REBUILD_QUERY_RESULTS_BATCH_SIZE);
055    
056                            if (treeModels.isEmpty()) {
057                                    continue;
058                            }
059    
060                            if (treeModels.size() ==
061                                            _MODEL_TREE_REBUILD_QUERY_RESULTS_BATCH_SIZE) {
062    
063                                    TreeModel treeModel = treeModels.get(treeModels.size() - 1);
064    
065                                    trace[2] = treeModel.getPrimaryKeyObj();
066    
067                                    traces.push(trace);
068                            }
069    
070                            for (TreeModel treeModel : treeModels) {
071                                    String treePath = curParentTreePath.concat(
072                                            String.valueOf(treeModel.getPrimaryKeyObj())).concat(
073                                                    StringPool.SLASH);
074    
075                                    treeModel.updateTreePath(treePath);
076    
077                                    traces.push(
078                                            new Object[] {treeModel.getPrimaryKeyObj(), treePath, 0L});
079    
080                                    modifiedTreeModels.add(treeModel);
081                            }
082                    }
083    
084                    treeModelTasks.reindexTreeModels(modifiedTreeModels);
085            }
086    
087            private static final int _MODEL_TREE_REBUILD_QUERY_RESULTS_BATCH_SIZE =
088                    GetterUtil.getInteger(
089                            PropsUtil.get(
090                                    PropsKeys.MODEL_TREE_REBUILD_QUERY_RESULTS_BATCH_SIZE));
091    
092    }