Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Marc Grünewald
Get all Portlets of a Site
May 29, 2012 3:23 AM
Answer

Marc Grünewald

Rank: Junior Member

Posts: 45

Join Date: March 20, 2012

Recent Posts

Hello everybody,

I'm trying to create a Site Template with three Pages and Default Portlets on each of them. Later I want to automatically add this Template to every Site I create inside a Custom Portlet. This works fine except for one little thing that keeps me from getting it done. On two of the Pages are variuos Asset Publisher with a Scope set to only the Current Site/Group. E.g. if my Template is calls "DefaultTemplate" I set the scope to "DefaultTemplate". But I want this Scope to be set to the Site/Group which uses the Template later on. Until now, whenever I assign a Template to a Site, the Scope remains "DefaultTemplate" but it should be "NewSite1" or whatever my new Site is called. After some investigation I found out, that by using
1
2List<KeyValuePair> scopesLeftList = new ArrayList<KeyValuePair>();
3
4            for (long groupId : groupIds) {
5                Group group = GroupLocalServiceUtil.getGroup(groupId);
6
7                scopesLeftList.add(new KeyValuePair(_getKey(group), _getName(group, locale)));
8            }

(which I found inside the configuration.jsp of the Asset Publisher) I can add the current Group to the Scope. Now I have the problem mentioned in this thread:
http://www.liferay.com/de/community/forums/-/message_boards/message/953134
which sadly never was resolved. I could get every Portlet on the current viewed Page, iterate over those and set the scope respectively using the code mentioned in the Thread. But I can't get the Portlets on the other two Pages..
So now I either need to know how to get the Portlets from ALL the Site Pages or how to set a dynamic variable inside the Template which would be set to the New Site.
Hope you guys/girls can help.

Sorry for the sometimes crappy English.

Best Regards

Marc
Marc Grünewald
RE: Get all Portlets of a Site
May 29, 2012 4:32 AM
Answer

Marc Grünewald

Rank: Junior Member

Posts: 45

Join Date: March 20, 2012

Recent Posts

Why is it, that I always find the answer after posting my problem on the Forums... This is solved for me.

For anyone whos interested in how I did it:

It's a pretty ugly solution, so don't blame me.
I fetched a List of every Layout/Page in the DB by using
1List<Layout> allLayouts = LayoutLocalServiceUtil.getLayouts(-1, -1);

After that I iterated over the List and only kept those which had the GroupId of my Site. Of course you can get this List by using:
1List<Layout> allLayouts = LayoutLocalServiceUtil.getLayouts(groupId, privateLayout);

From here on I was able to get the Portlets for each page seperately by using
 1for (Layout layout : allLayouts) {
 2                if (layout.getGroupId() == m_projectsModelBean
 3                    .getSelectedProject().getLrGroupId()) {
 4                    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
 5                        .getLayoutType();
 6                    LayoutTemplate layoutTemplate = layoutTypePortlet
 7                        .getLayoutTemplate();
 8                    List<String> columnCount = layoutTemplate.getColumns();
 9                    for (int i = 0; i < columnCount.size(); i++) {
10                        String column = columnCount.get(i);
11                        List<Portlet> portlets = layoutTypePortlet
12                            .getAllPortlets(column);
13                        for (Portlet portlet : portlets) {
14                            s_logger.info(portlet.getPortletName());  <----Do whatever you want with each Portlet
15                        }
16                    }
17                }
18            }
Marc Grünewald
RE: Get all Portlets of a Site
May 29, 2012 5:23 AM
Answer

Marc Grünewald

Rank: Junior Member

Posts: 45

Join Date: March 20, 2012

Recent Posts

Well it isn't solved actually, because I still can't figure out, how to set the new Scope programmatically. Any Idea on how to archive that?

Thanks in Advance

Best Regards

Marc
Nagendra Kumar Busam
RE: Get all Portlets of a Site
May 29, 2012 6:27 AM
Answer

Nagendra Kumar Busam

Rank: Liferay Master

Posts: 653

Join Date: July 7, 2009

Recent Posts

Hi Marc,

I tried the use case you are referring to manually (Instead of through API calls). It was working fine.

Try manually first, if it is working for you. Might be there is some problem with code -if you can share code snippet how you are trying to add, we can have a look into that

Thanks,
- Nagendra
Marc Grünewald
RE: Get all Portlets of a Site
May 29, 2012 7:10 AM
Answer

Marc Grünewald

Rank: Junior Member

Posts: 45

Join Date: March 20, 2012

Recent Posts

Hi, thanks for the quick reply. Yes, I tested it manually and it works fine, but as there are several Asset Publisher which all show different Content you would have to change the Scope for each Asset Publisher every time a new Site is created which isn't really User friendly I think. I'll give an proper answer with some Code tomorrow as I have to leave now.

Best Regards

Marc
Marc Grünewald
RE: Get all Portlets of a Site
May 30, 2012 4:42 AM
Answer

Marc Grünewald

Rank: Junior Member

Posts: 45

Join Date: March 20, 2012

Recent Posts

Okay so here's a long answer and a possible Solution I think.

First the Code to add a Layout Template to a Site:
 1
 2    private void addDefaultLayout(Group newGroup) throws Exception {
 3
 4        MethodKey key = new MethodKey(
 5            "com.liferay.portlet.sites.util.SitesUtil",
 6            "applyLayoutSetPrototypes", Group.class, long.class, long.class,
 7            ServiceContext.class);
 8        List<LayoutSetPrototype> allLayoutPrototypes = LayoutSetPrototypeLocalServiceUtil
 9            .getLayoutSetPrototypes(-1, -1);
10        LayoutSetPrototype projectDefaultLayout = null;
11        for (LayoutSetPrototype layoutSetPrototype : allLayoutPrototypes) {
12            if (layoutSetPrototype.getName().contains("ProjectDefault")) {
13                projectDefaultLayout = layoutSetPrototype;
14                s_logger.info("Found Template with id: "
15                    + layoutSetPrototype.getLayoutSetPrototypeId());
16
17                break;
18            }
19        }
20
21        PortalClassInvoker.invoke(true, key, newGroup,
22            projectDefaultLayout.getLayoutSetPrototypeId(), 0,
23            m_liferayFacesContext.getServiceContext());
24    }


This Method invokes the SitesUtil Class as it's not available in the Portal Service JAR. After that I get every LayputPrototype and fetch the one I need. Then I invoke the Method in the SitesUtil Class and that basically does the trick. It takes about 5-7 seconds because some DB Queries are beeing processed. But now for the actual Problem I had:

As I wrote above I had the Problem that the scope wasn't set to the new Group Id which was pretty annoying. If found out, that this is related to a Bug: LPS-25860 and LPS-27246. In the later Link a guy posted a possible Workaround which solved my Problem. I wrote a simple Hook using his modified jsp-File. If someone wants to use it, it's in the Attachments of the later Link too. So.. Problem solved! Thanks for the help!

Cheers Marc
Nagendra Kumar Busam
RE: Get all Portlets of a Site
May 30, 2012 7:25 AM
Answer

Nagendra Kumar Busam

Rank: Liferay Master

Posts: 653

Join Date: July 7, 2009

Recent Posts

Glad it's been resolved, thank you very much for sharing