001    /**
002     * Copyright (c) 2000-2012 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.lar;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.model.ClassedModel;
020    import com.liferay.portal.model.StagedModel;
021    
022    import java.io.Serializable;
023    
024    /**
025     * @author Mate Thurzo
026     * @author Daniel Kocsis
027     */
028    public class StagedModelPathUtil {
029    
030            public static String getPath(
031                    PortletDataContext portletDataContext, String className, long classPK) {
032    
033                    return getPath(portletDataContext, className, classPK, null);
034            }
035    
036            public static String getPath(
037                    PortletDataContext portletDataContext, String className, long classPK,
038                    String dependentFileName) {
039    
040                    return getPath(
041                            portletDataContext.getSourceGroupId(), className, classPK,
042                            dependentFileName);
043            }
044    
045            public static String getPath(StagedModel stagedModel) {
046                    return getPath(stagedModel, null);
047            }
048    
049            public static String getPath(
050                    StagedModel stagedModel, String dependentFileName) {
051    
052                    ClassedModel classedModel = (ClassedModel)stagedModel;
053    
054                    return getPath(
055                            stagedModel.getGroupId(), classedModel.getModelClassName(),
056                            classedModel.getPrimaryKeyObj(), dependentFileName);
057            }
058    
059            protected static String getPath(
060                    long groupId, String className, Serializable primaryKeyObj,
061                    String dependentFileName) {
062    
063                    StringBundler sb = new StringBundler(7);
064    
065                    sb.append("/groups/");
066                    sb.append(groupId);
067                    sb.append(StringPool.FORWARD_SLASH);
068                    sb.append(className);
069                    sb.append(StringPool.FORWARD_SLASH);
070                    sb.append(primaryKeyObj.toString());
071    
072                    if (dependentFileName == null) {
073                            sb.append(".xml");
074                    }
075                    else {
076                            sb.append(StringPool.FORWARD_SLASH);
077                            sb.append(dependentFileName);
078                    }
079    
080                    return sb.toString();
081            }
082    
083    }