Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Morad Ahmad
Create Sites programatically
August 17, 2011 8:04 AM
Answer

Morad Ahmad

Rank: Junior Member

Posts: 62

Join Date: June 16, 2010

Recent Posts

Hello,

ich try to write a hook which creates for each city in configuration file a new Site (with a site template). But i can't find any Lifery Service to do that. Somthing like
SiteLocalService.addSite(sitename, siteTemplate, ...)

Thanks,
Morad.
Jelmer Kuperus
RE: Create Sites programatically
August 17, 2011 12:26 PM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

Something like this

1ServiceContext serviceContext = new ServiceContext();
2
3Group group = groupLocalService.addGroup(userId, Group.class.getName(), 0,
4        name, description, GroupConstants.TYPE_COMMUNITY_OPEN,
5        friendlyUrl, true, serviceContext);
6       
7CommunitiesUtil.applyLayoutSetPrototypes(group,
8        publicLayoutSetPrototypeId, privateLayoutSetPrototypeId);



The problem however is that CommunitiesUtil is in portal impl so you need to cheat like this :

 1
 2import com.liferay.portal.kernel.exception.PortalException;
 3import com.liferay.portal.kernel.util.MethodKey;
 4import com.liferay.portal.kernel.util.PortalClassInvoker;
 5import com.liferay.portal.model.Group;
 6
 7/**
 8 * Classloader proxy to com.liferay.portlet.communities.util.CommunitiesUtil. Here because CommunitiesUtil is not in
 9 * portal-service.jar and we still need to access it from a plugin.
10 *
11 * @author Jelmer Kuperus
12 */
13public class CommunitiesUtil {
14
15    private static final String DELEGATE_CLASS_NAME =
16            "com.liferay.portlet.communities.util.CommunitiesUtil";
17
18    /**
19     * Private constructor to prevent initialization
20     */
21    private CommunitiesUtil() {
22    }
23
24    /**
25     * Apply layoutset prototypes to a group. Layout site prototypes are site templates. eg. a collection of
26     * preconfigured pages and components used to quickly create a site.
27     *
28     * @param group the groups to apply the prototypes to
29     * @param publicLayoutSetPrototypeId uniquely identifies a LayoutSetPrototype
30     * @param privateLayoutSetPrototypeId uniquely identifies a LayoutSetPrototype
31     * @throws Exception in case of an exception
32     */
33    public static void applyLayoutSetPrototypes(
34            Group group, long publicLayoutSetPrototypeId, long privateLayoutSetPrototypeId)
35        throws Exception {
36
37        MethodKey key = methodKeyFor("applyLayoutSetPrototypes", Group.class, long.class, long.class);
38        invokePortalClassMethod(key, group, publicLayoutSetPrototypeId, privateLayoutSetPrototypeId);
39    }
40
41    private static MethodKey methodKeyFor(String methodName, Class<?>... parameterTypes) {
42        return new MethodKey(DELEGATE_CLASS_NAME, methodName, parameterTypes);
43    }
44
45    private static Object invokePortalClassMethod(MethodKey key, Object... arguments) throws PortalException {
46        try {
47            //noinspection unchecked
48            return PortalClassInvoker.invoke(false, key, arguments);
49        } catch (PortalException e) {
50            throw e;
51        } catch (Exception e) {
52            throw new RuntimeException(e);
53        }
54    }
55
56}