001    /**
002     * Copyright (c) 2000-2013 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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.json.JSONArray;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.jsonwebservice.JSONWebService;
023    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceMode;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.model.PortletApp;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.base.PortletServiceBaseImpl;
029    
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    @JSONWebService(mode = JSONWebServiceMode.MANUAL)
036    public class PortletServiceImpl extends PortletServiceBaseImpl {
037    
038            @Override
039            public JSONArray getWARPortlets() {
040                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
041    
042                    List<Portlet> portlets = portletLocalService.getPortlets();
043    
044                    for (Portlet portlet : portlets) {
045                            PortletApp portletApp = portlet.getPortletApp();
046    
047                            if (portletApp.isWARFile()) {
048                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
049    
050                                    jsonObject.put("portlet_name", portlet.getPortletName());
051                                    jsonObject.put(
052                                            "servlet_context_name", portletApp.getServletContextName());
053    
054                                    jsonArray.put(jsonObject);
055                            }
056                    }
057    
058                    return jsonArray;
059            }
060    
061            @Override
062            public Portlet updatePortlet(
063                            long companyId, String portletId, String roles, boolean active)
064                    throws PortalException, SystemException {
065    
066                    if (!roleLocalService.hasUserRole(
067                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
068    
069                            throw new PrincipalException();
070                    }
071    
072                    return portletLocalService.updatePortlet(
073                            companyId, portletId, roles, active);
074            }
075    
076    }