Source Code Cross Referenced for LayoutLocalServiceImpl.java in  » Portal » liferay-portal-4.4.2 » com » liferay » portal » service » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » liferay portal 4.4.2 » com.liferay.portal.service.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003:         *
0004:         * Permission is hereby granted, free of charge, to any person obtaining a copy
0005:         * of this software and associated documentation files (the "Software"), to deal
0006:         * in the Software without restriction, including without limitation the rights
0007:         * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008:         * copies of the Software, and to permit persons to whom the Software is
0009:         * furnished to do so, subject to the following conditions:
0010:         *
0011:         * The above copyright notice and this permission notice shall be included in
0012:         * all copies or substantial portions of the Software.
0013:         *
0014:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015:         * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016:         * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017:         * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018:         * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019:         * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020:         * SOFTWARE.
0021:         */package com.liferay.portal.service.impl;
0022:
0023:        import com.liferay.portal.LayoutFriendlyURLException;
0024:        import com.liferay.portal.LayoutHiddenException;
0025:        import com.liferay.portal.LayoutImportException;
0026:        import com.liferay.portal.LayoutParentLayoutIdException;
0027:        import com.liferay.portal.LayoutTypeException;
0028:        import com.liferay.portal.NoSuchLayoutException;
0029:        import com.liferay.portal.NoSuchPortletPreferencesException;
0030:        import com.liferay.portal.PortalException;
0031:        import com.liferay.portal.RequiredLayoutException;
0032:        import com.liferay.portal.SystemException;
0033:        import com.liferay.portal.comm.CommLink;
0034:        import com.liferay.portal.kernel.language.LanguageUtil;
0035:        import com.liferay.portal.kernel.lar.PortletDataContext;
0036:        import com.liferay.portal.kernel.lar.PortletDataHandler;
0037:        import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
0038:        import com.liferay.portal.kernel.lar.UserIdStrategy;
0039:        import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
0040:        import com.liferay.portal.kernel.util.GetterUtil;
0041:        import com.liferay.portal.kernel.util.LocaleUtil;
0042:        import com.liferay.portal.kernel.util.MethodWrapper;
0043:        import com.liferay.portal.kernel.util.ReleaseInfo;
0044:        import com.liferay.portal.kernel.util.StringPool;
0045:        import com.liferay.portal.kernel.util.StringUtil;
0046:        import com.liferay.portal.kernel.util.Validator;
0047:        import com.liferay.portal.kernel.zip.ZipReader;
0048:        import com.liferay.portal.kernel.zip.ZipWriter;
0049:        import com.liferay.portal.lar.AlwaysCurrentUserIdStrategy;
0050:        import com.liferay.portal.lar.CurrentUserIdStrategy;
0051:        import com.liferay.portal.lar.PortletDataContextImpl;
0052:        import com.liferay.portal.model.Group;
0053:        import com.liferay.portal.model.Layout;
0054:        import com.liferay.portal.model.LayoutReference;
0055:        import com.liferay.portal.model.LayoutSet;
0056:        import com.liferay.portal.model.LayoutTypePortlet;
0057:        import com.liferay.portal.model.Portlet;
0058:        import com.liferay.portal.model.PortletPreferences;
0059:        import com.liferay.portal.model.Resource;
0060:        import com.liferay.portal.model.Role;
0061:        import com.liferay.portal.model.Theme;
0062:        import com.liferay.portal.model.User;
0063:        import com.liferay.portal.model.impl.ColorSchemeImpl;
0064:        import com.liferay.portal.model.impl.GroupImpl;
0065:        import com.liferay.portal.model.impl.LayoutImpl;
0066:        import com.liferay.portal.model.impl.PortletImpl;
0067:        import com.liferay.portal.model.impl.ResourceImpl;
0068:        import com.liferay.portal.security.permission.ResourceActionsUtil;
0069:        import com.liferay.portal.service.LayoutLocalServiceUtil;
0070:        import com.liferay.portal.service.base.LayoutLocalServiceBaseImpl;
0071:        import com.liferay.portal.service.permission.PortletPermissionUtil;
0072:        import com.liferay.portal.theme.ThemeLoader;
0073:        import com.liferay.portal.theme.ThemeLoaderFactory;
0074:        import com.liferay.portal.util.ContentUtil;
0075:        import com.liferay.portal.util.PortalUtil;
0076:        import com.liferay.portal.util.PortletKeys;
0077:        import com.liferay.portal.util.PropsValues;
0078:        import com.liferay.portal.util.comparator.LayoutPriorityComparator;
0079:        import com.liferay.portal.velocity.VelocityContextPool;
0080:        import com.liferay.portlet.PortletPreferencesImpl;
0081:        import com.liferay.portlet.PortletPreferencesSerializer;
0082:        import com.liferay.portlet.documentlibrary.NoSuchFolderException;
0083:        import com.liferay.portlet.documentlibrary.model.DLFolder;
0084:        import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
0085:        import com.liferay.util.CollectionFactory;
0086:        import com.liferay.util.FileUtil;
0087:        import com.liferay.util.MapUtil;
0088:        import com.liferay.util.Normalizer;
0089:        import com.liferay.util.Time;
0090:        import com.liferay.util.xml.XMLFormatter;
0091:
0092:        import com.thoughtworks.xstream.XStream;
0093:
0094:        import java.io.ByteArrayInputStream;
0095:        import java.io.File;
0096:        import java.io.FileInputStream;
0097:        import java.io.FileNotFoundException;
0098:        import java.io.IOException;
0099:        import java.io.InputStream;
0100:        import java.io.StringReader;
0101:
0102:        import java.util.ArrayList;
0103:        import java.util.Collections;
0104:        import java.util.Date;
0105:        import java.util.HashMap;
0106:        import java.util.Iterator;
0107:        import java.util.LinkedHashMap;
0108:        import java.util.LinkedHashSet;
0109:        import java.util.List;
0110:        import java.util.Locale;
0111:        import java.util.Map;
0112:        import java.util.Properties;
0113:        import java.util.Set;
0114:
0115:        import javax.servlet.ServletContext;
0116:
0117:        import org.apache.commons.lang.time.StopWatch;
0118:        import org.apache.commons.logging.Log;
0119:        import org.apache.commons.logging.LogFactory;
0120:
0121:        import org.dom4j.Document;
0122:        import org.dom4j.DocumentHelper;
0123:        import org.dom4j.Element;
0124:        import org.dom4j.io.SAXReader;
0125:
0126:        /**
0127:         * <a href="LayoutLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
0128:         *
0129:         * @author Brian Wing Shun Chan
0130:         * @author Joel Kozikowski
0131:         * @author Charles May
0132:         * @author Raymond Augé
0133:         * @author Jorge Ferrer
0134:         * @author Bruno Farache
0135:         *
0136:         */
0137:        public class LayoutLocalServiceImpl extends LayoutLocalServiceBaseImpl {
0138:
0139:            public Layout addLayout(long userId, long groupId,
0140:                    boolean privateLayout, long parentLayoutId, String name,
0141:                    String title, String description, String type,
0142:                    boolean hidden, String friendlyURL) throws PortalException,
0143:                    SystemException {
0144:
0145:                Map localeNamesMap = new HashMap();
0146:
0147:                Locale defaultLocale = LocaleUtil.getDefault();
0148:
0149:                localeNamesMap.put(defaultLocale, name);
0150:
0151:                return addLayout(userId, groupId, privateLayout,
0152:                        parentLayoutId, localeNamesMap, new HashMap(),
0153:                        description, type, hidden, friendlyURL);
0154:            }
0155:
0156:            public Layout addLayout(long userId, long groupId,
0157:                    boolean privateLayout, long parentLayoutId,
0158:                    Map localeNamesMap, Map localeTitlesMap,
0159:                    String description, String type, boolean hidden,
0160:                    String friendlyURL) throws PortalException, SystemException {
0161:
0162:                return addLayout(userId, groupId, privateLayout,
0163:                        parentLayoutId, localeNamesMap, localeTitlesMap,
0164:                        description, type, hidden, friendlyURL,
0165:                        DLFolderImpl.DEFAULT_PARENT_FOLDER_ID);
0166:            }
0167:
0168:            public Layout addLayout(long userId, long groupId,
0169:                    boolean privateLayout, long parentLayoutId, String name,
0170:                    String title, String description, String type,
0171:                    boolean hidden, String friendlyURL, long dlFolderId)
0172:                    throws PortalException, SystemException {
0173:
0174:                Map localeNamesMap = new HashMap();
0175:
0176:                Locale defaultLocale = LocaleUtil.getDefault();
0177:
0178:                localeNamesMap.put(defaultLocale, name);
0179:
0180:                return addLayout(userId, groupId, privateLayout,
0181:                        parentLayoutId, localeNamesMap, new HashMap(),
0182:                        description, type, hidden, friendlyURL, dlFolderId);
0183:            }
0184:
0185:            public Layout addLayout(long userId, long groupId,
0186:                    boolean privateLayout, long parentLayoutId,
0187:                    Map localeNamesMap, Map localeTitlesMap,
0188:                    String description, String type, boolean hidden,
0189:                    String friendlyURL, long dlFolderId)
0190:                    throws PortalException, SystemException {
0191:
0192:                // Layout
0193:
0194:                User user = userPersistence.findByPrimaryKey(userId);
0195:                long layoutId = getNextLayoutId(groupId, privateLayout);
0196:                parentLayoutId = getParentLayoutId(groupId, privateLayout,
0197:                        parentLayoutId);
0198:                friendlyURL = getFriendlyURL(friendlyURL);
0199:                int priority = getNextPriority(groupId, privateLayout,
0200:                        parentLayoutId);
0201:
0202:                validate(groupId, privateLayout, layoutId, parentLayoutId,
0203:                        type, hidden, friendlyURL);
0204:
0205:                long plid = counterLocalService.increment();
0206:
0207:                Layout layout = layoutPersistence.create(plid);
0208:
0209:                layout.setGroupId(groupId);
0210:                layout.setCompanyId(user.getCompanyId());
0211:                layout.setPrivateLayout(privateLayout);
0212:                layout.setLayoutId(layoutId);
0213:                layout.setParentLayoutId(parentLayoutId);
0214:                layout.setDescription(description);
0215:                layout.setType(type);
0216:                layout.setHidden(hidden);
0217:                layout.setFriendlyURL(friendlyURL);
0218:                layout.setPriority(priority);
0219:                layout.setDlFolderId(dlFolderId);
0220:
0221:                setLocalizedAttributes(layout, localeNamesMap, localeTitlesMap);
0222:
0223:                layoutPersistence.update(layout);
0224:
0225:                // Resources
0226:
0227:                resourceLocalService.addResources(user.getCompanyId(), groupId,
0228:                        user.getUserId(), Layout.class.getName(), layout
0229:                                .getPlid(), false, true, true);
0230:
0231:                // Layout set
0232:
0233:                layoutSetLocalService.updatePageCount(groupId, privateLayout);
0234:
0235:                return layout;
0236:            }
0237:
0238:            public void deleteLayout(long plid) throws PortalException,
0239:                    SystemException {
0240:
0241:                Layout layout = layoutPersistence.findByPrimaryKey(plid);
0242:
0243:                deleteLayout(layout, true);
0244:            }
0245:
0246:            public void deleteLayout(long groupId, boolean privateLayout,
0247:                    long layoutId) throws PortalException, SystemException {
0248:
0249:                Layout layout = layoutPersistence.findByG_P_L(groupId,
0250:                        privateLayout, layoutId);
0251:
0252:                deleteLayout(layout, true);
0253:            }
0254:
0255:            public void deleteLayout(Layout layout, boolean updateLayoutSet)
0256:                    throws PortalException, SystemException {
0257:
0258:                // Child layouts
0259:
0260:                List childLayouts = layoutPersistence.findByG_P_P(layout
0261:                        .getGroupId(), layout.isPrivateLayout(), layout
0262:                        .getLayoutId());
0263:
0264:                for (int i = 0; i < childLayouts.size(); i++) {
0265:                    Layout childLayout = (Layout) childLayouts.get(i);
0266:
0267:                    deleteLayout(childLayout, updateLayoutSet);
0268:                }
0269:
0270:                // Portlet preferences
0271:
0272:                portletPreferencesLocalService.deletePortletPreferences(
0273:                        PortletKeys.PREFS_OWNER_ID_DEFAULT,
0274:                        PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid());
0275:
0276:                // Ratings
0277:
0278:                ratingsStatsLocalService.deleteStats(Layout.class.getName(),
0279:                        layout.getPlid());
0280:
0281:                // Message boards
0282:
0283:                mbMessageLocalService.deleteDiscussionMessages(Layout.class
0284:                        .getName(), layout.getPlid());
0285:
0286:                // Journal content searches
0287:
0288:                journalContentSearchLocalService.deleteLayoutContentSearches(
0289:                        layout.getGroupId(), layout.isPrivateLayout(), layout
0290:                                .getLayoutId());
0291:
0292:                // Icon
0293:
0294:                ImageLocalUtil.deleteImage(layout.getIconImageId());
0295:
0296:                // Resources
0297:
0298:                String primKey = layout.getPlid()
0299:                        + PortletImpl.LAYOUT_SEPARATOR + "%";
0300:
0301:                Iterator itr = resourceFinder.findByC_P(layout.getCompanyId(),
0302:                        primKey).iterator();
0303:
0304:                while (itr.hasNext()) {
0305:                    Resource resource = (Resource) itr.next();
0306:
0307:                    resourceLocalService.deleteResource(resource);
0308:                }
0309:
0310:                resourceLocalService.deleteResource(layout.getCompanyId(),
0311:                        Layout.class.getName(), ResourceImpl.SCOPE_INDIVIDUAL,
0312:                        layout.getPlid());
0313:
0314:                // Layout
0315:
0316:                layoutPersistence.remove(layout.getPlid());
0317:
0318:                // Layout set
0319:
0320:                if (updateLayoutSet) {
0321:                    layoutSetLocalService.updatePageCount(layout.getGroupId(),
0322:                            layout.isPrivateLayout());
0323:                }
0324:            }
0325:
0326:            public void deleteLayouts(long groupId, boolean privateLayout)
0327:                    throws PortalException, SystemException {
0328:
0329:                // Layouts
0330:
0331:                List layouts = layoutPersistence.findByG_P_P(groupId,
0332:                        privateLayout, LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
0333:
0334:                Iterator itr = layouts.iterator();
0335:
0336:                while (itr.hasNext()) {
0337:                    Layout layout = (Layout) itr.next();
0338:
0339:                    try {
0340:                        deleteLayout(layout, false);
0341:                    } catch (NoSuchLayoutException nsle) {
0342:                    }
0343:                }
0344:
0345:                // Layout set
0346:
0347:                layoutSetLocalService.updatePageCount(groupId, privateLayout);
0348:            }
0349:
0350:            public byte[] exportLayouts(long groupId, boolean privateLayout,
0351:                    Map parameterMap) throws PortalException, SystemException {
0352:
0353:                return exportLayouts(groupId, privateLayout, null, parameterMap);
0354:            }
0355:
0356:            public byte[] exportLayouts(long groupId, boolean privateLayout,
0357:                    long[] layoutIds, Map parameterMap) throws PortalException,
0358:                    SystemException {
0359:
0360:                boolean exportPermissions = MapUtil.getBoolean(parameterMap,
0361:                        PortletDataHandlerKeys.PERMISSIONS);
0362:                boolean exportUserPermissions = MapUtil.getBoolean(
0363:                        parameterMap, PortletDataHandlerKeys.USER_PERMISSIONS);
0364:                boolean exportPortletData = MapUtil.getBoolean(parameterMap,
0365:                        PortletDataHandlerKeys.PORTLET_DATA);
0366:                boolean exportPortletSetup = MapUtil.getBoolean(parameterMap,
0367:                        PortletDataHandlerKeys.PORTLET_SETUP);
0368:                boolean exportPortletUserPreferences = MapUtil.getBoolean(
0369:                        parameterMap,
0370:                        PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
0371:                boolean exportTheme = MapUtil.getBoolean(parameterMap,
0372:                        PortletDataHandlerKeys.THEME);
0373:
0374:                if (_log.isDebugEnabled()) {
0375:                    _log.debug("Export permissions " + exportPermissions);
0376:                    _log.debug("Export user permissions "
0377:                            + exportUserPermissions);
0378:                    _log.debug("Export portlet data " + exportPortletData);
0379:                    _log.debug("Export portlet setup " + exportPortletSetup);
0380:                    _log.debug("Export portlet user preferences "
0381:                            + exportPortletUserPreferences);
0382:                    _log.debug("Export theme " + exportTheme);
0383:                }
0384:
0385:                StopWatch stopWatch = null;
0386:
0387:                if (_log.isInfoEnabled()) {
0388:                    stopWatch = new StopWatch();
0389:
0390:                    stopWatch.start();
0391:                }
0392:
0393:                LayoutCache layoutCache = new LayoutCache();
0394:
0395:                LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
0396:                        groupId, privateLayout);
0397:
0398:                long companyId = layoutSet.getCompanyId();
0399:                long defaultUserId = userLocalService
0400:                        .getDefaultUserId(companyId);
0401:
0402:                ZipWriter zipWriter = new ZipWriter();
0403:
0404:                PortletDataContext context = new PortletDataContextImpl(
0405:                        companyId, groupId, parameterMap, CollectionFactory
0406:                                .getHashSet(), zipWriter);
0407:
0408:                Group guestGroup = groupLocalService.getGroup(companyId,
0409:                        GroupImpl.GUEST);
0410:
0411:                // Build compatibility
0412:
0413:                Document doc = DocumentHelper.createDocument();
0414:
0415:                Element root = doc.addElement("root");
0416:
0417:                Element header = root.addElement("header");
0418:
0419:                header.addAttribute("build-number", String.valueOf(ReleaseInfo
0420:                        .getBuildNumber()));
0421:                header.addAttribute("group-id", String.valueOf(groupId));
0422:                header.addAttribute("private-layout", String
0423:                        .valueOf(privateLayout));
0424:                header.addAttribute("export-date", Time.getRFC822());
0425:                header.addAttribute("theme-id", layoutSet.getThemeId());
0426:                header.addAttribute("color-scheme-id", layoutSet
0427:                        .getColorSchemeId());
0428:                header.addAttribute("type", "layouts");
0429:
0430:                // Layouts
0431:
0432:                Set portletIds = new LinkedHashSet();
0433:
0434:                List layouts = null;
0435:
0436:                if ((layoutIds == null) || (layoutIds.length == 0)) {
0437:                    layouts = getLayouts(groupId, privateLayout);
0438:                } else {
0439:                    layouts = getLayouts(groupId, privateLayout, layoutIds);
0440:                }
0441:
0442:                Iterator itr = layouts.iterator();
0443:
0444:                while (itr.hasNext()) {
0445:                    Layout layout = (Layout) itr.next();
0446:
0447:                    context.setPlid(layout.getPlid());
0448:
0449:                    Element layoutEl = root.addElement("layout");
0450:
0451:                    layoutEl.addAttribute("layout-id", String.valueOf(layout
0452:                            .getLayoutId()));
0453:                    layoutEl.addElement("parent-layout-id").addText(
0454:                            String.valueOf(layout.getParentLayoutId()));
0455:                    layoutEl.addElement("name").addCDATA(layout.getName());
0456:                    layoutEl.addElement("title").addCDATA(layout.getTitle());
0457:                    layoutEl.addElement("type").addText(layout.getType());
0458:                    layoutEl.addElement("type-settings").addCDATA(
0459:                            layout.getTypeSettings());
0460:                    layoutEl.addElement("hidden").addText(
0461:                            String.valueOf(layout.getHidden()));
0462:                    layoutEl.addElement("friendly-url").addText(
0463:                            layout.getFriendlyURL());
0464:                    layoutEl.addElement("theme-id")
0465:                            .addText(layout.getThemeId());
0466:                    layoutEl.addElement("color-scheme-id").addText(
0467:                            layout.getColorSchemeId());
0468:                    layoutEl.addElement("priority").addText(
0469:                            String.valueOf(layout.getPriority()));
0470:
0471:                    Element permissionsEl = layoutEl.addElement("permissions");
0472:
0473:                    // Layout permissions
0474:
0475:                    if (exportPermissions) {
0476:                        exportLayoutPermissions(layoutCache, companyId,
0477:                                groupId, guestGroup, layout, permissionsEl,
0478:                                exportUserPermissions);
0479:                    }
0480:
0481:                    if (layout.getType().equals(LayoutImpl.TYPE_PORTLET)) {
0482:                        LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
0483:                                .getLayoutType();
0484:
0485:                        // The order of the export is important. You must always export
0486:                        // the portlet data first, then the portlet preferences, then
0487:                        // the portlet permissions. The export of the portlet data
0488:                        // process may modify the portlet preferences, and those changes
0489:                        // should be included in the exported LAR.
0490:
0491:                        // Portlet data
0492:
0493:                        if (exportPortletData) {
0494:                            exportPortletData(context, layout,
0495:                                    layoutTypePortlet, layoutEl);
0496:                        }
0497:
0498:                        // Portlet preferences
0499:
0500:                        if (exportPortletSetup) {
0501:                            exportPortletPreferences(
0502:                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
0503:                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false,
0504:                                    layout.getPlid(), layoutTypePortlet,
0505:                                    layoutEl);
0506:
0507:                            exportPortletPreferences(layout.getGroupId(),
0508:                                    PortletKeys.PREFS_OWNER_TYPE_GROUP, false,
0509:                                    layout.getPlid(), layoutTypePortlet,
0510:                                    layoutEl);
0511:
0512:                            exportPortletPreferences(layout.getCompanyId(),
0513:                                    PortletKeys.PREFS_OWNER_TYPE_COMPANY,
0514:                                    false, layout.getPlid(), layoutTypePortlet,
0515:                                    layoutEl);
0516:                        }
0517:
0518:                        if (exportPortletUserPreferences) {
0519:                            exportPortletPreferences(defaultUserId,
0520:                                    PortletKeys.PREFS_OWNER_TYPE_USER, true,
0521:                                    layout.getPlid(), layoutTypePortlet,
0522:                                    layoutEl);
0523:                        }
0524:
0525:                        // Portlet permissions
0526:
0527:                        if (exportPermissions) {
0528:                            exportPortletPermissions(layoutCache, companyId,
0529:                                    groupId, guestGroup, layout,
0530:                                    layoutTypePortlet, portletIds,
0531:                                    permissionsEl, exportUserPermissions);
0532:                        }
0533:                    }
0534:                }
0535:
0536:                // Portlet preferences
0537:
0538:                if (exportPortletUserPreferences) {
0539:                    exportPortletPreferences(groupId,
0540:                            PortletKeys.PREFS_OWNER_TYPE_GROUP,
0541:                            PortletKeys.PREFS_PLID_SHARED, false, root);
0542:
0543:                }
0544:
0545:                Element rolesEl = root.addElement("roles");
0546:
0547:                // Layout roles
0548:
0549:                if (exportPermissions) {
0550:                    exportLayoutRoles(layoutCache, companyId, groupId, rolesEl);
0551:                }
0552:
0553:                // Portlet roles
0554:
0555:                if (exportPermissions) {
0556:                    exportPortletRoles(layoutCache, companyId, groupId,
0557:                            portletIds, rolesEl);
0558:                }
0559:
0560:                // Comments
0561:
0562:                exportComments(context, root);
0563:
0564:                // Ratings
0565:
0566:                exportRatings(context, root);
0567:
0568:                // Tags
0569:
0570:                exportTags(context, root);
0571:
0572:                // Look and feel
0573:
0574:                byte[] themeZip = null;
0575:
0576:                try {
0577:                    if (exportTheme) {
0578:                        themeZip = exportTheme(layoutSet);
0579:                    }
0580:                } catch (IOException ioe) {
0581:                    throw new SystemException(ioe);
0582:                }
0583:
0584:                // XML
0585:
0586:                if (_log.isInfoEnabled()) {
0587:                    _log.info("Exporting layouts takes " + stopWatch.getTime()
0588:                            + " ms");
0589:                }
0590:
0591:                // Zip
0592:
0593:                try {
0594:                    zipWriter.addEntry("layouts.xml", XMLFormatter
0595:                            .toString(doc));
0596:
0597:                    if (themeZip != null) {
0598:                        zipWriter.addEntry("theme.zip", themeZip);
0599:                    }
0600:
0601:                    return zipWriter.finish();
0602:                } catch (IOException ioe) {
0603:                    throw new SystemException(ioe);
0604:                }
0605:            }
0606:
0607:            public byte[] exportPortletInfo(long plid, String portletId,
0608:                    Map parameterMap) throws PortalException, SystemException {
0609:
0610:                boolean exportPortletSetup = MapUtil.getBoolean(parameterMap,
0611:                        PortletDataHandlerKeys.PORTLET_SETUP);
0612:                boolean exportPortletUserPreferences = MapUtil.getBoolean(
0613:                        parameterMap,
0614:                        PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
0615:
0616:                StopWatch stopWatch = null;
0617:
0618:                if (_log.isInfoEnabled()) {
0619:                    stopWatch = new StopWatch();
0620:
0621:                    stopWatch.start();
0622:                }
0623:
0624:                Layout layout = LayoutLocalServiceUtil.getLayout(plid);
0625:
0626:                if (!layout.getType().equals(LayoutImpl.TYPE_PORTLET)) {
0627:                    throw new LayoutImportException("Layout type "
0628:                            + layout.getType() + " is not valid");
0629:                }
0630:
0631:                LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
0632:                        .getLayoutType();
0633:
0634:                if (!layoutTypePortlet.hasPortletId(portletId)) {
0635:                    throw new LayoutImportException(
0636:                            "The specified layout does not have portlet "
0637:                                    + portletId);
0638:                }
0639:
0640:                long companyId = layout.getCompanyId();
0641:                long defaultUserId = userLocalService
0642:                        .getDefaultUserId(companyId);
0643:
0644:                ZipWriter zipWriter = new ZipWriter();
0645:
0646:                PortletDataContext context = new PortletDataContextImpl(
0647:                        companyId, layout.getGroupId(), parameterMap,
0648:                        CollectionFactory.getHashSet(), zipWriter);
0649:
0650:                context.setPlid(plid);
0651:
0652:                // Build compatibility
0653:
0654:                Document doc = DocumentHelper.createDocument();
0655:
0656:                Element root = doc.addElement("root");
0657:
0658:                Element header = root.addElement("header");
0659:
0660:                header.addAttribute("build-number", String.valueOf(ReleaseInfo
0661:                        .getBuildNumber()));
0662:                header.addAttribute("group-id", String.valueOf(layout
0663:                        .getGroupId()));
0664:                header.addAttribute("export-date", Time.getRFC822());
0665:                header.addAttribute("type", "portlet");
0666:
0667:                // Data
0668:
0669:                PortletPreferences portletPreferences = (PortletPreferences) portletPreferencesLocalService
0670:                        .getPortletPreferences(plid).get(0);
0671:
0672:                javax.portlet.PortletPreferences jxPrefs = portletPreferencesLocalService
0673:                        .getPreferences(layout.getCompanyId(),
0674:                                portletPreferences.getOwnerId(),
0675:                                portletPreferences.getOwnerType(), plid,
0676:                                portletId);
0677:
0678:                exportPortletData(context, portletId, jxPrefs, root);
0679:
0680:                // Comments
0681:
0682:                exportComments(context, root);
0683:
0684:                // Ratings
0685:
0686:                exportRatings(context, root);
0687:
0688:                // Tags
0689:
0690:                exportTags(context, root);
0691:
0692:                // Portlet preferences
0693:
0694:                if (exportPortletSetup) {
0695:                    exportPortletPreferences(
0696:                            PortletKeys.PREFS_OWNER_ID_DEFAULT,
0697:                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT, false, layout,
0698:                            portletId, root);
0699:
0700:                    exportPortletPreferences(layout.getGroupId(),
0701:                            PortletKeys.PREFS_OWNER_TYPE_GROUP, false, layout,
0702:                            portletId, root);
0703:
0704:                    exportPortletPreferences(layout.getCompanyId(),
0705:                            PortletKeys.PREFS_OWNER_TYPE_COMPANY, false,
0706:                            layout, portletId, root);
0707:                }
0708:
0709:                if (exportPortletUserPreferences) {
0710:                    exportPortletPreferences(defaultUserId,
0711:                            PortletKeys.PREFS_OWNER_TYPE_USER, true, layout,
0712:                            portletId, root);
0713:                }
0714:
0715:                // XML
0716:
0717:                if (_log.isInfoEnabled()) {
0718:                    _log.info("Exporting portlet info takes "
0719:                            + stopWatch.getTime() + " ms");
0720:                }
0721:
0722:                // Zip
0723:
0724:                try {
0725:                    zipWriter.addEntry("portlet.xml", XMLFormatter
0726:                            .toString(doc));
0727:
0728:                    return zipWriter.finish();
0729:                } catch (IOException ioe) {
0730:                    throw new SystemException(ioe);
0731:                }
0732:            }
0733:
0734:            public long getDefaultPlid(long groupId) throws SystemException {
0735:                if (groupId > 0) {
0736:                    List layouts = layoutPersistence.findByGroupId(groupId, 0,
0737:                            1);
0738:
0739:                    if (layouts.size() > 0) {
0740:                        Layout layout = (Layout) layouts.get(0);
0741:
0742:                        return layout.getPlid();
0743:                    }
0744:                }
0745:
0746:                return LayoutImpl.DEFAULT_PLID;
0747:            }
0748:
0749:            public long getDefaultPlid(long groupId, boolean privateLayout)
0750:                    throws SystemException {
0751:
0752:                if (groupId > 0) {
0753:                    List layouts = layoutPersistence.findByG_P(groupId,
0754:                            privateLayout, 0, 1);
0755:
0756:                    if (layouts.size() > 0) {
0757:                        Layout layout = (Layout) layouts.get(0);
0758:
0759:                        return layout.getPlid();
0760:                    }
0761:                }
0762:
0763:                return LayoutImpl.DEFAULT_PLID;
0764:            }
0765:
0766:            public Layout getDLFolderLayout(long dlFolderId)
0767:                    throws PortalException, SystemException {
0768:
0769:                return layoutPersistence.findByDLFolderId(dlFolderId);
0770:            }
0771:
0772:            public Layout getFriendlyURLLayout(long groupId,
0773:                    boolean privateLayout, String friendlyURL)
0774:                    throws PortalException, SystemException {
0775:
0776:                if (Validator.isNull(friendlyURL)) {
0777:                    throw new NoSuchLayoutException();
0778:                }
0779:
0780:                friendlyURL = getFriendlyURL(friendlyURL);
0781:
0782:                return layoutPersistence.findByG_P_F(groupId, privateLayout,
0783:                        friendlyURL);
0784:            }
0785:
0786:            public Layout getLayout(long plid) throws PortalException,
0787:                    SystemException {
0788:
0789:                return layoutPersistence.findByPrimaryKey(plid);
0790:            }
0791:
0792:            public Layout getLayout(long groupId, boolean privateLayout,
0793:                    long layoutId) throws PortalException, SystemException {
0794:
0795:                return layoutPersistence.findByG_P_L(groupId, privateLayout,
0796:                        layoutId);
0797:            }
0798:
0799:            public Layout getLayoutByIconImageId(long iconImageId)
0800:                    throws PortalException, SystemException {
0801:
0802:                return layoutPersistence.findByIconImageId(iconImageId);
0803:            }
0804:
0805:            public List getLayouts(long groupId, boolean privateLayout)
0806:                    throws SystemException {
0807:
0808:                return layoutPersistence.findByG_P(groupId, privateLayout);
0809:            }
0810:
0811:            public List getLayouts(long groupId, boolean privateLayout,
0812:                    long parentLayoutId) throws SystemException {
0813:
0814:                return layoutPersistence.findByG_P_P(groupId, privateLayout,
0815:                        parentLayoutId);
0816:            }
0817:
0818:            public List getLayouts(long groupId, boolean privateLayout,
0819:                    long parentLayoutId, int begin, int end)
0820:                    throws SystemException {
0821:
0822:                return layoutPersistence.findByG_P_P(groupId, privateLayout,
0823:                        parentLayoutId, begin, end);
0824:            }
0825:
0826:            public List getLayouts(long groupId, boolean privateLayout,
0827:                    long[] layoutIds) throws PortalException, SystemException {
0828:
0829:                List layouts = new ArrayList();
0830:
0831:                for (int i = 0; i < layoutIds.length; i++) {
0832:                    Layout layout = getLayout(groupId, privateLayout,
0833:                            layoutIds[i]);
0834:
0835:                    layouts.add(layout);
0836:                }
0837:
0838:                return layouts;
0839:            }
0840:
0841:            public LayoutReference[] getLayouts(long companyId,
0842:                    String portletId, String prefsKey, String prefsValue)
0843:                    throws SystemException {
0844:
0845:                List list = layoutFinder.findByC_P_P(companyId, portletId,
0846:                        prefsKey, prefsValue);
0847:
0848:                return (LayoutReference[]) list.toArray(new LayoutReference[0]);
0849:            }
0850:
0851:            public void importLayouts(long userId, long groupId,
0852:                    boolean privateLayout, Map parameterMap, File file)
0853:                    throws PortalException, SystemException {
0854:
0855:                try {
0856:                    importLayouts(userId, groupId, privateLayout, parameterMap,
0857:                            new FileInputStream(file));
0858:                } catch (FileNotFoundException fnfe) {
0859:                    throw new SystemException(fnfe);
0860:                }
0861:            }
0862:
0863:            public void importLayouts(long userId, long groupId,
0864:                    boolean privateLayout, Map parameterMap, InputStream is)
0865:                    throws PortalException, SystemException {
0866:
0867:                boolean deleteMissingLayouts = MapUtil.getBoolean(parameterMap,
0868:                        PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
0869:                        Boolean.TRUE.booleanValue());
0870:                boolean deletePortletData = MapUtil.getBoolean(parameterMap,
0871:                        PortletDataHandlerKeys.DELETE_PORTLET_DATA);
0872:                boolean importPermissions = MapUtil.getBoolean(parameterMap,
0873:                        PortletDataHandlerKeys.PERMISSIONS);
0874:                boolean importUserPermissions = MapUtil.getBoolean(
0875:                        parameterMap, PortletDataHandlerKeys.PERMISSIONS);
0876:                boolean importPortletData = MapUtil.getBoolean(parameterMap,
0877:                        PortletDataHandlerKeys.PORTLET_DATA);
0878:                boolean importPortletSetup = MapUtil.getBoolean(parameterMap,
0879:                        PortletDataHandlerKeys.PORTLET_SETUP);
0880:                boolean importPortletUserPreferences = MapUtil.getBoolean(
0881:                        parameterMap,
0882:                        PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
0883:                boolean importTheme = MapUtil.getBoolean(parameterMap,
0884:                        PortletDataHandlerKeys.THEME);
0885:                String userIdStrategy = MapUtil.getString(parameterMap,
0886:                        PortletDataHandlerKeys.USER_ID_STRATEGY);
0887:
0888:                if (_log.isDebugEnabled()) {
0889:                    _log.debug("Delete portlet data " + deletePortletData);
0890:                    _log.debug("Import permissions " + importPermissions);
0891:                    _log.debug("Import user permissions "
0892:                            + importUserPermissions);
0893:                    _log.debug("Import portlet data " + importPortletData);
0894:                    _log.debug("Import portlet setup " + importPortletSetup);
0895:                    _log.debug("Import portlet user preferences "
0896:                            + importPortletUserPreferences);
0897:                    _log.debug("Import theme " + importTheme);
0898:                }
0899:
0900:                StopWatch stopWatch = null;
0901:
0902:                if (_log.isInfoEnabled()) {
0903:                    stopWatch = new StopWatch();
0904:
0905:                    stopWatch.start();
0906:                }
0907:
0908:                LayoutCache layoutCache = new LayoutCache();
0909:
0910:                LayoutSet layoutSet = layoutSetLocalService.getLayoutSet(
0911:                        groupId, privateLayout);
0912:
0913:                long companyId = layoutSet.getCompanyId();
0914:
0915:                User user = userPersistence.findByPrimaryKey(userId);
0916:
0917:                // User ID strategy
0918:
0919:                UserIdStrategy strategy = null;
0920:
0921:                if (UserIdStrategy.ALWAYS_CURRENT_USER_ID
0922:                        .equals(userIdStrategy)) {
0923:                    strategy = new AlwaysCurrentUserIdStrategy(user);
0924:                } else if (UserIdStrategy.CURRENT_USER_ID
0925:                        .equals(userIdStrategy)) {
0926:                    strategy = new CurrentUserIdStrategy(user);
0927:                }
0928:
0929:                ZipReader zipReader = new ZipReader(is);
0930:
0931:                PortletDataContext context = new PortletDataContextImpl(
0932:                        companyId, groupId, parameterMap, CollectionFactory
0933:                                .getHashSet(), strategy, zipReader);
0934:
0935:                Group guestGroup = groupLocalService.getGroup(companyId,
0936:                        GroupImpl.GUEST);
0937:
0938:                // Zip
0939:
0940:                Element root = null;
0941:                byte[] themeZip = null;
0942:
0943:                try {
0944:
0945:                    // XML
0946:
0947:                    String xml = zipReader.getEntryAsString("layouts.xml");
0948:
0949:                    SAXReader reader = new SAXReader();
0950:
0951:                    Document doc = reader.read(new StringReader(xml));
0952:
0953:                    root = doc.getRootElement();
0954:
0955:                    // Look and feel
0956:
0957:                    if (importTheme) {
0958:                        themeZip = zipReader.getEntryAsByteArray("theme.zip");
0959:                    }
0960:                } catch (Exception e) {
0961:                    throw new SystemException(e);
0962:                }
0963:
0964:                // Build compatibility
0965:
0966:                Element header = (Element) root.element("header");
0967:
0968:                int buildNumber = ReleaseInfo.getBuildNumber();
0969:
0970:                int importBuildNumber = GetterUtil.getInteger(header
0971:                        .attributeValue("build-number"));
0972:
0973:                if (buildNumber != importBuildNumber) {
0974:                    throw new LayoutImportException("LAR build number "
0975:                            + importBuildNumber + " does not match "
0976:                            + "portal build number " + buildNumber);
0977:                }
0978:
0979:                // Look and feel
0980:
0981:                String themeId = header.attributeValue("theme-id");
0982:                String colorSchemeId = header.attributeValue("color-scheme-id");
0983:
0984:                boolean useThemeZip = false;
0985:
0986:                if (themeZip != null) {
0987:                    try {
0988:                        String importThemeId = importTheme(layoutSet, themeZip);
0989:
0990:                        if (importThemeId != null) {
0991:                            themeId = importThemeId;
0992:                            colorSchemeId = ColorSchemeImpl
0993:                                    .getDefaultRegularColorSchemeId();
0994:
0995:                            useThemeZip = true;
0996:                        }
0997:
0998:                        if (_log.isDebugEnabled()) {
0999:                            _log.debug("Importing theme takes "
1000:                                    + stopWatch.getTime() + " ms");
1001:                        }
1002:                    } catch (Exception e) {
1003:                        throw new SystemException(e);
1004:                    }
1005:                }
1006:
1007:                boolean wapTheme = false;
1008:
1009:                layoutSetLocalService.updateLookAndFeel(groupId, privateLayout,
1010:                        themeId, colorSchemeId, StringPool.BLANK, wapTheme);
1011:
1012:                // Read comments, ratings, and tags to make them available to the data
1013:                // handlers through the context
1014:
1015:                readComments(context, root);
1016:                readRatings(context, root);
1017:                readTags(context, root);
1018:
1019:                // Layouts
1020:
1021:                Set newLayoutIds = CollectionFactory.getHashSet();
1022:
1023:                Iterator itr = root.elements("layout").iterator();
1024:
1025:                if (_log.isDebugEnabled()) {
1026:                    if (itr.hasNext()) {
1027:                        _log.debug("Importing layouts");
1028:                    }
1029:                }
1030:
1031:                while (itr.hasNext()) {
1032:                    Element layoutEl = (Element) itr.next();
1033:
1034:                    long layoutId = GetterUtil.getInteger(layoutEl
1035:                            .attributeValue("layout-id"));
1036:                    long parentLayoutId = GetterUtil.getInteger(layoutEl
1037:                            .elementText("parent-layout-id"));
1038:
1039:                    if (_log.isDebugEnabled()) {
1040:                        _log.debug("Importing layout with layout id "
1041:                                + layoutId + " and parent layout id "
1042:                                + parentLayoutId);
1043:                    }
1044:
1045:                    String name = layoutEl.elementText("name");
1046:                    String title = layoutEl.elementText("title");
1047:                    String type = layoutEl.elementText("type");
1048:                    String typeSettings = layoutEl.elementText("type-settings");
1049:                    boolean hidden = GetterUtil.getBoolean(layoutEl
1050:                            .elementText("hidden"));
1051:                    String friendlyURL = layoutEl.elementText("friendly-url");
1052:
1053:                    if (useThemeZip) {
1054:                        themeId = StringPool.BLANK;
1055:                        colorSchemeId = StringPool.BLANK;
1056:                    } else {
1057:                        themeId = layoutEl.elementText("theme-id");
1058:                        colorSchemeId = layoutEl.elementText("color-scheme-id");
1059:                    }
1060:
1061:                    int priority = GetterUtil.getInteger(layoutEl
1062:                            .elementText("priority"));
1063:
1064:                    Layout layout = layoutPersistence.fetchByG_P_L(groupId,
1065:                            privateLayout, layoutId);
1066:
1067:                    if (_log.isDebugEnabled()) {
1068:                        if (layout == null) {
1069:                            _log.debug("Layout with {groupId=" + groupId
1070:                                    + ",privateLayout=" + privateLayout
1071:                                    + ",layoutId=" + layoutId
1072:                                    + "} does not exist");
1073:                        } else {
1074:                            _log.debug("Layout with {groupId=" + groupId
1075:                                    + ",privateLayout=" + privateLayout
1076:                                    + ",layoutId=" + layoutId + "} exists");
1077:                        }
1078:                    }
1079:
1080:                    if (layout == null) {
1081:                        long plid = counterLocalService.increment();
1082:
1083:                        layout = layoutPersistence.create(plid);
1084:
1085:                        layout.setGroupId(groupId);
1086:                        layout.setPrivateLayout(privateLayout);
1087:                        layout.setLayoutId(layoutId);
1088:                    }
1089:
1090:                    layout.setCompanyId(user.getCompanyId());
1091:                    layout.setParentLayoutId(parentLayoutId);
1092:                    layout.setName(name);
1093:                    layout.setTitle(title);
1094:                    layout.setType(type);
1095:                    layout.setTypeSettings(typeSettings);
1096:                    layout.setHidden(hidden);
1097:                    layout.setFriendlyURL(friendlyURL);
1098:                    layout.setThemeId(themeId);
1099:                    layout.setColorSchemeId(colorSchemeId);
1100:                    layout.setPriority(priority);
1101:
1102:                    fixTypeSettings(layout);
1103:
1104:                    layoutPersistence.update(layout);
1105:
1106:                    context.setPlid(layout.getPlid());
1107:
1108:                    newLayoutIds.add(new Long(layoutId));
1109:
1110:                    Element permissionsEl = layoutEl.element("permissions");
1111:
1112:                    // Layout permissions
1113:
1114:                    if (importPermissions) {
1115:                        importLayoutPermissions(layoutCache, companyId,
1116:                                groupId, guestGroup, layout, permissionsEl,
1117:                                importUserPermissions);
1118:                    }
1119:
1120:                    // The order of the import is important. You must always import
1121:                    // the portlet preferences first, then the portlet data, then
1122:                    // the portlet permissions. The import of the portlet data
1123:                    // assumes that portlet preferences already exist.
1124:
1125:                    // Delete portlet data
1126:
1127:                    if (deletePortletData) {
1128:                        deletePortletData(context, layout, layoutEl);
1129:                    }
1130:
1131:                    // Portlet preferences
1132:
1133:                    importPortletPreferences(layoutSet.getCompanyId(), layout
1134:                            .getPlid(), layoutEl, importPortletSetup,
1135:                            importPortletUserPreferences);
1136:
1137:                    // Portlet data
1138:
1139:                    if (importPortletData) {
1140:                        importPortletData(context, layout, layoutEl);
1141:                    }
1142:
1143:                    // Portlet permissions
1144:
1145:                    if (importPermissions) {
1146:                        importPortletPermissions(layoutCache, companyId,
1147:                                groupId, guestGroup, layout, permissionsEl,
1148:                                importUserPermissions);
1149:                    }
1150:                }
1151:
1152:                Element rolesEl = (Element) root.element("roles");
1153:
1154:                // Layout roles
1155:
1156:                if (importPermissions) {
1157:                    importLayoutRoles(layoutCache, companyId, groupId, rolesEl);
1158:                }
1159:
1160:                // Portlet roles
1161:
1162:                if (importPermissions) {
1163:                    importPortletRoles(layoutCache, companyId, groupId, rolesEl);
1164:                }
1165:
1166:                // Delete missing layouts
1167:
1168:                if (deleteMissingLayouts) {
1169:                    deleteMissingLayouts(groupId, privateLayout, newLayoutIds);
1170:                }
1171:
1172:                // Page count
1173:
1174:                layoutSetLocalService.updatePageCount(groupId, privateLayout);
1175:
1176:                if (_log.isInfoEnabled()) {
1177:                    _log.info("Importing layouts takes " + stopWatch.getTime()
1178:                            + " ms");
1179:                }
1180:            }
1181:
1182:            public void importPortletInfo(long userId, long plid,
1183:                    String portletId, Map parameterMap, File file)
1184:                    throws PortalException, SystemException {
1185:
1186:                try {
1187:                    importPortletInfo(userId, plid, portletId, parameterMap,
1188:                            new FileInputStream(file));
1189:                } catch (FileNotFoundException fnfe) {
1190:                    throw new SystemException(fnfe);
1191:                }
1192:            }
1193:
1194:            public void importPortletInfo(long userId, long plid,
1195:                    String portletId, Map parameterMap, InputStream is)
1196:                    throws PortalException, SystemException {
1197:
1198:                boolean deletePortletData = MapUtil.getBoolean(parameterMap,
1199:                        PortletDataHandlerKeys.DELETE_PORTLET_DATA);
1200:                boolean importPortletData = MapUtil.getBoolean(parameterMap,
1201:                        PortletDataHandlerKeys.PORTLET_DATA);
1202:                boolean importPortletSetup = MapUtil.getBoolean(parameterMap,
1203:                        PortletDataHandlerKeys.PORTLET_SETUP);
1204:                boolean importUserPreferences = MapUtil.getBoolean(
1205:                        parameterMap,
1206:                        PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
1207:                String userIdStrategy = MapUtil.getString(parameterMap,
1208:                        PortletDataHandlerKeys.USER_ID_STRATEGY);
1209:
1210:                StopWatch stopWatch = null;
1211:
1212:                if (_log.isInfoEnabled()) {
1213:                    stopWatch = new StopWatch();
1214:
1215:                    stopWatch.start();
1216:                }
1217:
1218:                Layout layout = LayoutLocalServiceUtil.getLayout(plid);
1219:
1220:                long companyId = layout.getCompanyId();
1221:
1222:                User user = userPersistence.findByPrimaryKey(userId);
1223:
1224:                // User ID strategy
1225:
1226:                UserIdStrategy strategy = null;
1227:
1228:                if (UserIdStrategy.ALWAYS_CURRENT_USER_ID
1229:                        .equals(userIdStrategy)) {
1230:                    strategy = new AlwaysCurrentUserIdStrategy(user);
1231:                } else if (UserIdStrategy.CURRENT_USER_ID
1232:                        .equals(userIdStrategy)) {
1233:                    strategy = new CurrentUserIdStrategy(user);
1234:                }
1235:
1236:                ZipReader zipReader = new ZipReader(is);
1237:
1238:                PortletDataContext context = new PortletDataContextImpl(
1239:                        companyId, layout.getGroupId(), parameterMap,
1240:                        CollectionFactory.getHashSet(), strategy, zipReader);
1241:
1242:                context.setPlid(plid);
1243:
1244:                // Zip
1245:
1246:                Element root = null;
1247:
1248:                try {
1249:
1250:                    // XML
1251:
1252:                    String xml = zipReader.getEntryAsString("portlet.xml");
1253:
1254:                    SAXReader reader = new SAXReader();
1255:
1256:                    Document doc = reader.read(new StringReader(xml));
1257:
1258:                    root = doc.getRootElement();
1259:                } catch (Exception e) {
1260:                    throw new SystemException(e);
1261:                }
1262:
1263:                // Build compatibility
1264:
1265:                Element header = (Element) root.element("header");
1266:
1267:                int buildNumber = ReleaseInfo.getBuildNumber();
1268:
1269:                int importBuildNumber = GetterUtil.getInteger(header
1270:                        .attributeValue("build-number"));
1271:
1272:                if (buildNumber != importBuildNumber) {
1273:                    throw new LayoutImportException("LAR build number "
1274:                            + importBuildNumber + " does not match "
1275:                            + "portal build number " + buildNumber);
1276:                }
1277:
1278:                String type = header.attributeValue("type");
1279:
1280:                if (!type.equals("portlet")) {
1281:                    throw new LayoutImportException(
1282:                            "Invalid type of LAR file (" + type + ")");
1283:                }
1284:
1285:                // Read comments, ratings, and tags to make them available to the data
1286:                // handlers through the context
1287:
1288:                readComments(context, root);
1289:                readRatings(context, root);
1290:                readTags(context, root);
1291:
1292:                // Delete portlet data
1293:
1294:                if (_log.isDebugEnabled()) {
1295:                    _log.debug("Deleting portlet data");
1296:                }
1297:
1298:                if (deletePortletData) {
1299:                    deletePortletData(context, layout, root);
1300:                }
1301:
1302:                // Portlet preferences
1303:
1304:                importPortletPreferences(layout.getCompanyId(), plid, root,
1305:                        importPortletSetup, importUserPreferences);
1306:
1307:                // Portlet data
1308:
1309:                if (_log.isDebugEnabled()) {
1310:                    _log.debug("Importing portlet data");
1311:                }
1312:
1313:                if (importPortletData) {
1314:                    importPortletData(context, layout, root);
1315:                }
1316:
1317:                if (_log.isInfoEnabled()) {
1318:                    _log.info("Importing portlet data takes "
1319:                            + stopWatch.getTime() + " ms");
1320:                }
1321:            }
1322:
1323:            public void setLayouts(long groupId, boolean privateLayout,
1324:                    long parentLayoutId, long[] layoutIds)
1325:                    throws PortalException, SystemException {
1326:
1327:                if (layoutIds == null) {
1328:                    return;
1329:                }
1330:
1331:                if (parentLayoutId == LayoutImpl.DEFAULT_PARENT_LAYOUT_ID) {
1332:                    if (layoutIds.length < 1) {
1333:                        throw new RequiredLayoutException(
1334:                                RequiredLayoutException.AT_LEAST_ONE);
1335:                    }
1336:
1337:                    Layout layout = layoutPersistence.findByG_P_L(groupId,
1338:                            privateLayout, layoutIds[0]);
1339:
1340:                    if (!layout.getType().equals(LayoutImpl.TYPE_PORTLET)) {
1341:                        throw new RequiredLayoutException(
1342:                                RequiredLayoutException.FIRST_LAYOUT_TYPE);
1343:                    }
1344:
1345:                    if (layout.isHidden()) {
1346:                        throw new RequiredLayoutException(
1347:                                RequiredLayoutException.FIRST_LAYOUT_HIDDEN);
1348:                    }
1349:                }
1350:
1351:                Set layoutIdsSet = new LinkedHashSet();
1352:
1353:                for (int i = 0; i < layoutIds.length; i++) {
1354:                    layoutIdsSet.add(new Long(layoutIds[i]));
1355:                }
1356:
1357:                Set newLayoutIdsSet = CollectionFactory.getHashSet();
1358:
1359:                Iterator itr = layoutPersistence.findByG_P_P(groupId,
1360:                        privateLayout, parentLayoutId).iterator();
1361:
1362:                while (itr.hasNext()) {
1363:                    Layout layout = (Layout) itr.next();
1364:
1365:                    Long layoutIdObj = new Long(layout.getLayoutId());
1366:
1367:                    if (!layoutIdsSet.contains(layoutIdObj)) {
1368:                        deleteLayout(layout, true);
1369:                    } else {
1370:                        newLayoutIdsSet.add(layoutIdObj);
1371:                    }
1372:                }
1373:
1374:                int priority = 0;
1375:
1376:                itr = layoutIdsSet.iterator();
1377:
1378:                while (itr.hasNext()) {
1379:                    Long layoutIdObj = (Long) itr.next();
1380:
1381:                    Layout layout = layoutPersistence.findByG_P_L(groupId,
1382:                            privateLayout, layoutIdObj.longValue());
1383:
1384:                    layout.setPriority(priority++);
1385:
1386:                    layoutPersistence.update(layout);
1387:                }
1388:
1389:                layoutSetLocalService.updatePageCount(groupId, privateLayout);
1390:            }
1391:
1392:            public Layout updateLayout(long groupId, boolean privateLayout,
1393:                    long layoutId, long parentLayoutId, Map localeNamesMap,
1394:                    Map localeTitlesMap, String description, String type,
1395:                    boolean hidden, String friendlyURL) throws PortalException,
1396:                    SystemException {
1397:
1398:                return updateLayout(groupId, privateLayout, layoutId,
1399:                        parentLayoutId, localeNamesMap, localeTitlesMap,
1400:                        description, type, hidden, friendlyURL, null, null);
1401:            }
1402:
1403:            public Layout updateLayout(long groupId, boolean privateLayout,
1404:                    long layoutId, long parentLayoutId, Map localeNamesMap,
1405:                    Map localeTitlesMap, String description, String type,
1406:                    boolean hidden, String friendlyURL, Boolean iconImage,
1407:                    byte[] iconBytes) throws PortalException, SystemException {
1408:
1409:                // Layout
1410:
1411:                parentLayoutId = getParentLayoutId(groupId, privateLayout,
1412:                        parentLayoutId);
1413:                friendlyURL = getFriendlyURL(friendlyURL);
1414:
1415:                validate(groupId, privateLayout, layoutId, parentLayoutId,
1416:                        type, hidden, friendlyURL);
1417:
1418:                validateParentLayoutId(groupId, privateLayout, layoutId,
1419:                        parentLayoutId);
1420:
1421:                Layout layout = layoutPersistence.findByG_P_L(groupId,
1422:                        privateLayout, layoutId);
1423:
1424:                if (parentLayoutId != layout.getParentLayoutId()) {
1425:                    layout.setPriority(getNextPriority(groupId, privateLayout,
1426:                            parentLayoutId));
1427:                }
1428:
1429:                layout.setParentLayoutId(parentLayoutId);
1430:                layout.setDescription(description);
1431:                layout.setType(type);
1432:                layout.setHidden(hidden);
1433:                layout.setFriendlyURL(friendlyURL);
1434:
1435:                setLocalizedAttributes(layout, localeNamesMap, localeTitlesMap);
1436:
1437:                if (iconImage != null) {
1438:                    layout.setIconImage(iconImage.booleanValue());
1439:
1440:                    if (iconImage.booleanValue()) {
1441:                        long iconImageId = layout.getIconImageId();
1442:
1443:                        if (iconImageId <= 0) {
1444:                            iconImageId = counterLocalService.increment();
1445:
1446:                            layout.setIconImageId(iconImageId);
1447:                        }
1448:                    }
1449:                }
1450:
1451:                layoutPersistence.update(layout);
1452:
1453:                // Icon
1454:
1455:                if (iconImage != null) {
1456:                    if (!iconImage.booleanValue()) {
1457:                        ImageLocalUtil.deleteImage(layout.getIconImageId());
1458:                    } else if ((iconBytes != null) && (iconBytes.length > 0)) {
1459:                        ImageLocalUtil.updateImage(layout.getIconImageId(),
1460:                                iconBytes);
1461:                    }
1462:                }
1463:
1464:                try {
1465:                    if (layout.getDlFolderId() > 0) {
1466:                        DLFolder folder = dlFolderLocalService.getFolder(layout
1467:                                .getDlFolderId());
1468:
1469:                        dlFolderLocalService.updateFolder(folder.getFolderId(),
1470:                                folder.getParentFolderId(), layout
1471:                                        .getName(LocaleUtil.getDefault()),
1472:                                folder.getDescription());
1473:                    }
1474:                } catch (NoSuchFolderException nsfe) {
1475:                }
1476:
1477:                return layout;
1478:            }
1479:
1480:            public Layout updateLayout(long groupId, boolean privateLayout,
1481:                    long layoutId, String typeSettings) throws PortalException,
1482:                    SystemException {
1483:
1484:                Layout layout = layoutPersistence.findByG_P_L(groupId,
1485:                        privateLayout, layoutId);
1486:
1487:                layout.setTypeSettings(typeSettings);
1488:
1489:                layoutPersistence.update(layout);
1490:
1491:                return layout;
1492:            }
1493:
1494:            public Layout updateLookAndFeel(long groupId,
1495:                    boolean privateLayout, long layoutId, String themeId,
1496:                    String colorSchemeId, String css, boolean wapTheme)
1497:                    throws PortalException, SystemException {
1498:
1499:                Layout layout = layoutPersistence.findByG_P_L(groupId,
1500:                        privateLayout, layoutId);
1501:
1502:                if (wapTheme) {
1503:                    layout.setWapThemeId(themeId);
1504:                    layout.setWapColorSchemeId(colorSchemeId);
1505:                } else {
1506:                    layout.setThemeId(themeId);
1507:                    layout.setColorSchemeId(colorSchemeId);
1508:                    layout.setCss(css);
1509:                }
1510:
1511:                layoutPersistence.update(layout);
1512:
1513:                return layout;
1514:            }
1515:
1516:            public Layout updateName(long plid, String name, String languageId)
1517:                    throws PortalException, SystemException {
1518:
1519:                Layout layout = layoutPersistence.findByPrimaryKey(plid);
1520:
1521:                return updateName(layout, name, languageId);
1522:            }
1523:
1524:            public Layout updateName(long groupId, boolean privateLayout,
1525:                    long layoutId, String name, String languageId)
1526:                    throws PortalException, SystemException {
1527:
1528:                Layout layout = layoutPersistence.findByG_P_L(groupId,
1529:                        privateLayout, layoutId);
1530:
1531:                return updateName(layout, name, languageId);
1532:            }
1533:
1534:            public Layout updateName(Layout layout, String name,
1535:                    String languageId) throws PortalException, SystemException {
1536:
1537:                layout.setName(name, LocaleUtil.fromLanguageId(languageId));
1538:
1539:                layoutPersistence.update(layout);
1540:
1541:                try {
1542:                    if (layout.getDlFolderId() > 0) {
1543:                        DLFolder folder = dlFolderLocalService.getFolder(layout
1544:                                .getDlFolderId());
1545:
1546:                        dlFolderLocalService.updateFolder(folder.getFolderId(),
1547:                                folder.getParentFolderId(), layout
1548:                                        .getName(LocaleUtil.getDefault()),
1549:                                folder.getDescription());
1550:                    }
1551:                } catch (NoSuchFolderException nsfe) {
1552:                }
1553:
1554:                return layout;
1555:            }
1556:
1557:            public Layout updateParentLayoutId(long plid, long parentPlid)
1558:                    throws PortalException, SystemException {
1559:
1560:                Layout layout = layoutPersistence.findByPrimaryKey(plid);
1561:
1562:                long parentLayoutId = LayoutImpl.DEFAULT_PARENT_LAYOUT_ID;
1563:
1564:                if (parentPlid > 0) {
1565:                    try {
1566:                        Layout parentLayout = layoutPersistence
1567:                                .findByPrimaryKey(parentPlid);
1568:
1569:                        parentLayoutId = parentLayout.getLayoutId();
1570:                    } catch (NoSuchLayoutException nsle) {
1571:                    }
1572:                }
1573:
1574:                parentLayoutId = getParentLayoutId(layout.getGroupId(), layout
1575:                        .isPrivateLayout(), parentLayoutId);
1576:
1577:                validateParentLayoutId(layout.getGroupId(), layout
1578:                        .isPrivateLayout(), layout.getLayoutId(),
1579:                        parentLayoutId);
1580:
1581:                if (parentLayoutId != layout.getParentLayoutId()) {
1582:                    int priority = getNextPriority(layout.getGroupId(), layout
1583:                            .isPrivateLayout(), parentLayoutId);
1584:
1585:                    layout.setPriority(priority);
1586:                }
1587:
1588:                layout.setParentLayoutId(parentLayoutId);
1589:
1590:                layoutPersistence.update(layout);
1591:
1592:                return layout;
1593:            }
1594:
1595:            public Layout updateParentLayoutId(long groupId,
1596:                    boolean privateLayout, long layoutId, long parentLayoutId)
1597:                    throws PortalException, SystemException {
1598:
1599:                parentLayoutId = getParentLayoutId(groupId, privateLayout,
1600:                        parentLayoutId);
1601:
1602:                validateParentLayoutId(groupId, privateLayout, layoutId,
1603:                        parentLayoutId);
1604:
1605:                Layout layout = layoutPersistence.findByG_P_L(groupId,
1606:                        privateLayout, layoutId);
1607:
1608:                if (parentLayoutId != layout.getParentLayoutId()) {
1609:                    layout.setPriority(getNextPriority(groupId, privateLayout,
1610:                            parentLayoutId));
1611:                }
1612:
1613:                layout.setParentLayoutId(parentLayoutId);
1614:
1615:                layoutPersistence.update(layout);
1616:
1617:                return layout;
1618:            }
1619:
1620:            public Layout updatePriority(long plid, int priority)
1621:                    throws PortalException, SystemException {
1622:
1623:                Layout layout = layoutPersistence.findByPrimaryKey(plid);
1624:
1625:                return updatePriority(layout, priority);
1626:            }
1627:
1628:            public Layout updatePriority(long groupId, boolean privateLayout,
1629:                    long layoutId, int priority) throws PortalException,
1630:                    SystemException {
1631:
1632:                Layout layout = layoutPersistence.findByG_P_L(groupId,
1633:                        privateLayout, layoutId);
1634:
1635:                return updatePriority(layout, priority);
1636:            }
1637:
1638:            public Layout updatePriority(Layout layout, int priority)
1639:                    throws PortalException, SystemException {
1640:
1641:                if (layout.getPriority() == priority) {
1642:                    return layout;
1643:                }
1644:
1645:                boolean lessThan = false;
1646:
1647:                if (layout.getPriority() < priority) {
1648:                    lessThan = true;
1649:                }
1650:
1651:                layout.setPriority(priority);
1652:
1653:                layoutPersistence.update(layout);
1654:
1655:                priority = 0;
1656:
1657:                List layouts = layoutPersistence.findByG_P_P(layout
1658:                        .getGroupId(), layout.isPrivateLayout(), layout
1659:                        .getParentLayoutId());
1660:
1661:                Collections.sort(layouts, new LayoutPriorityComparator(layout,
1662:                        lessThan));
1663:
1664:                Iterator itr = layouts.iterator();
1665:
1666:                while (itr.hasNext()) {
1667:                    Layout curLayout = (Layout) itr.next();
1668:
1669:                    curLayout.setPriority(priority++);
1670:
1671:                    layoutPersistence.update(curLayout);
1672:
1673:                    if (curLayout.equals(layout)) {
1674:                        layout = curLayout;
1675:                    }
1676:                }
1677:
1678:                return layout;
1679:            }
1680:
1681:            protected void deleteMissingLayouts(long groupId,
1682:                    boolean privateLayout, Set newLayoutIds)
1683:                    throws PortalException, SystemException {
1684:
1685:                // Layouts
1686:
1687:                if (_log.isDebugEnabled()) {
1688:                    if (newLayoutIds.size() > 0) {
1689:                        _log.debug("Delete missing layouts");
1690:                    }
1691:                }
1692:
1693:                List layouts = layoutPersistence.findByG_P(groupId,
1694:                        privateLayout);
1695:
1696:                Iterator itr = layouts.iterator();
1697:
1698:                while (itr.hasNext()) {
1699:                    Layout layout = (Layout) itr.next();
1700:
1701:                    if (!newLayoutIds.contains(new Long(layout.getLayoutId()))) {
1702:                        try {
1703:                            deleteLayout(layout, false);
1704:                        } catch (NoSuchLayoutException nsle) {
1705:                        }
1706:                    }
1707:                }
1708:
1709:                // Layout set
1710:
1711:                layoutSetLocalService.updatePageCount(groupId, privateLayout);
1712:            }
1713:
1714:            protected void deletePortletData(PortletDataContext context,
1715:                    Layout layout, Element parentEl) throws PortalException,
1716:                    SystemException {
1717:
1718:                Iterator itr = parentEl.elements("portlet-data").iterator();
1719:
1720:                while (itr.hasNext()) {
1721:                    Element el = (Element) itr.next();
1722:
1723:                    String portletId = el.attributeValue("portlet-id");
1724:
1725:                    try {
1726:                        PortletPreferences portletPreferences = portletPreferencesPersistence
1727:                                .findByO_O_P_P(
1728:                                        PortletKeys.PREFS_OWNER_ID_DEFAULT,
1729:                                        PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
1730:                                        layout.getPlid(), portletId);
1731:
1732:                        String preferences = deletePortletData(context,
1733:                                portletId, portletPreferences, el);
1734:
1735:                        if (preferences != null) {
1736:                            portletPreferences.setPreferences(preferences);
1737:
1738:                            portletPreferencesPersistence
1739:                                    .update(portletPreferences);
1740:                        }
1741:                    } catch (NoSuchPortletPreferencesException nsppe) {
1742:                    }
1743:                }
1744:            }
1745:
1746:            protected String deletePortletData(PortletDataContext context,
1747:                    String portletId, PortletPreferences portletPreferences,
1748:                    Element parentEl) throws PortalException, SystemException {
1749:
1750:                Portlet portlet = portletLocalService.getPortletById(context
1751:                        .getCompanyId(), portletId);
1752:
1753:                if (portlet == null) {
1754:                    if (_log.isDebugEnabled()) {
1755:                        _log.debug("Do not delete portlet data for "
1756:                                + portletId
1757:                                + " because the portlet does not exist");
1758:                    }
1759:
1760:                    return null;
1761:                }
1762:
1763:                PortletDataHandler portletDataHandler = portlet
1764:                        .getPortletDataHandlerInstance();
1765:
1766:                if (portletDataHandler == null) {
1767:                    if (_log.isDebugEnabled()) {
1768:                        _log.debug("Do not delete portlet data for "
1769:                                + portletId
1770:                                + " because the portlet does not have a "
1771:                                + "PortletDataHandler");
1772:                    }
1773:
1774:                    return null;
1775:                }
1776:
1777:                if (_log.isDebugEnabled()) {
1778:                    _log.debug("Deleting data for " + portletId);
1779:                }
1780:
1781:                PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl) PortletPreferencesSerializer
1782:                        .fromDefaultXML(portletPreferences.getPreferences());
1783:
1784:                prefsImpl = (PortletPreferencesImpl) portletDataHandler
1785:                        .deleteData(context, portletId, prefsImpl);
1786:
1787:                if (prefsImpl == null) {
1788:                    return null;
1789:                }
1790:
1791:                return PortletPreferencesSerializer.toXML(prefsImpl);
1792:            }
1793:
1794:            protected void exportComments(PortletDataContext context,
1795:                    Element root) throws SystemException {
1796:
1797:                try {
1798:                    XStream xStream = new XStream();
1799:
1800:                    Map commentsMap = context.getComments();
1801:
1802:                    Iterator itr = commentsMap.keySet().iterator();
1803:
1804:                    while (itr.hasNext()) {
1805:                        String key = (String) itr.next();
1806:
1807:                        String[] comment = key.split(StringPool.POUND);
1808:
1809:                        Element el = root.addElement("comments");
1810:
1811:                        el.addAttribute("class-name", comment[0]);
1812:                        el.addAttribute("class-pk", comment[1]);
1813:
1814:                        List messages = (List) commentsMap.get(key);
1815:
1816:                        String xml = xStream.toXML(messages);
1817:
1818:                        Document tempDoc = PortalUtil.readDocumentFromXML(xml);
1819:
1820:                        el.content().add(tempDoc.getRootElement().createCopy());
1821:                    }
1822:                } catch (Exception e) {
1823:                    throw new SystemException(e);
1824:                }
1825:            }
1826:
1827:            protected Element exportGroupPermissions(long companyId,
1828:                    long groupId, String resourceName, String resourcePrimKey,
1829:                    Element parentEl, String elName) throws PortalException,
1830:                    SystemException {
1831:
1832:                Element el = parentEl.addElement(elName);
1833:
1834:                List permissions = permissionLocalService.getGroupPermissions(
1835:                        groupId, companyId, resourceName,
1836:                        ResourceImpl.SCOPE_INDIVIDUAL, resourcePrimKey);
1837:
1838:                List actions = ResourceActionsUtil.getActions(permissions);
1839:
1840:                for (int i = 0; i < actions.size(); i++) {
1841:                    String action = (String) actions.get(i);
1842:
1843:                    Element actionKeyEl = el.addElement("action-key");
1844:
1845:                    actionKeyEl.addText(action);
1846:                }
1847:
1848:                return el;
1849:            }
1850:
1851:            protected void exportGroupRoles(LayoutCache layoutCache,
1852:                    long companyId, long groupId, String resourceName,
1853:                    String entityName, Element parentEl)
1854:                    throws PortalException, SystemException {
1855:
1856:                List roles = layoutCache.getGroupRoles(groupId);
1857:
1858:                Element groupEl = exportRoles(companyId, resourceName,
1859:                        ResourceImpl.SCOPE_GROUP, String.valueOf(groupId),
1860:                        parentEl, entityName + "-roles", roles);
1861:
1862:                if (groupEl.elements().isEmpty()) {
1863:                    parentEl.remove(groupEl);
1864:                }
1865:            }
1866:
1867:            protected void exportInheritedPermissions(LayoutCache layoutCache,
1868:                    long companyId, String resourceName,
1869:                    String resourcePrimKey, Element parentEl, String entityName)
1870:                    throws PortalException, SystemException {
1871:
1872:                Element entityPermissionsEl = DocumentHelper
1873:                        .createElement(entityName + "-permissions");
1874:
1875:                Map entityMap = layoutCache.getEntityMap(companyId, entityName);
1876:
1877:                Iterator itr = entityMap.entrySet().iterator();
1878:
1879:                while (itr.hasNext()) {
1880:                    Map.Entry entry = (Map.Entry) itr.next();
1881:
1882:                    String name = entry.getKey().toString();
1883:
1884:                    long entityGroupId = ((Long) entry.getValue()).longValue();
1885:
1886:                    Element entityEl = exportGroupPermissions(companyId,
1887:                            entityGroupId, resourceName, resourcePrimKey,
1888:                            entityPermissionsEl, entityName + "-actions");
1889:
1890:                    if (entityEl.elements().isEmpty()) {
1891:                        entityPermissionsEl.remove(entityEl);
1892:                    } else {
1893:                        entityEl.addAttribute("name", name);
1894:                    }
1895:                }
1896:
1897:                if (!entityPermissionsEl.elements().isEmpty()) {
1898:                    parentEl.add(entityPermissionsEl);
1899:                }
1900:            }
1901:
1902:            protected void exportInheritedRoles(LayoutCache layoutCache,
1903:                    long companyId, long groupId, String resourceName,
1904:                    String entityName, Element parentEl)
1905:                    throws PortalException, SystemException {
1906:
1907:                Element entityRolesEl = DocumentHelper.createElement(entityName
1908:                        + "-roles");
1909:
1910:                Map entityMap = layoutCache.getEntityMap(companyId, entityName);
1911:
1912:                Iterator itr = entityMap.entrySet().iterator();
1913:
1914:                while (itr.hasNext()) {
1915:                    Map.Entry entry = (Map.Entry) itr.next();
1916:
1917:                    String name = entry.getKey().toString();
1918:
1919:                    long entityGroupId = ((Long) entry.getValue()).longValue();
1920:
1921:                    List entityRoles = layoutCache.getGroupRoles(entityGroupId);
1922:
1923:                    Element entityEl = exportRoles(companyId, resourceName,
1924:                            ResourceImpl.SCOPE_GROUP, String.valueOf(groupId),
1925:                            entityRolesEl, entityName, entityRoles);
1926:
1927:                    if (entityEl.elements().isEmpty()) {
1928:                        entityRolesEl.remove(entityEl);
1929:                    } else {
1930:                        entityEl.addAttribute("name", name);
1931:                    }
1932:                }
1933:
1934:                if (!entityRolesEl.elements().isEmpty()) {
1935:                    parentEl.add(entityRolesEl);
1936:                }
1937:            }
1938:
1939:            protected void exportLayoutPermissions(LayoutCache layoutCache,
1940:                    long companyId, long groupId, Group guestGroup,
1941:                    Layout layout, Element permissionsEl,
1942:                    boolean exportUserPermissions) throws PortalException,
1943:                    SystemException {
1944:
1945:                String resourceName = Layout.class.getName();
1946:                String resourcePrimKey = String.valueOf(layout.getPlid());
1947:
1948:                exportGroupPermissions(companyId, groupId, resourceName,
1949:                        resourcePrimKey, permissionsEl, "community-actions");
1950:
1951:                if (groupId != guestGroup.getGroupId()) {
1952:                    exportGroupPermissions(companyId, guestGroup.getGroupId(),
1953:                            resourceName, resourcePrimKey, permissionsEl,
1954:                            "guest-actions");
1955:                }
1956:
1957:                if (exportUserPermissions) {
1958:                    exportUserPermissions(layoutCache, companyId, groupId,
1959:                            resourceName, resourcePrimKey, permissionsEl);
1960:                }
1961:
1962:                exportInheritedPermissions(layoutCache, companyId,
1963:                        resourceName, resourcePrimKey, permissionsEl,
1964:                        "organization");
1965:
1966:                exportInheritedPermissions(layoutCache, companyId,
1967:                        resourceName, resourcePrimKey, permissionsEl,
1968:                        "location");
1969:
1970:                exportInheritedPermissions(layoutCache, companyId,
1971:                        resourceName, resourcePrimKey, permissionsEl,
1972:                        "user-group");
1973:            }
1974:
1975:            protected void exportLayoutRoles(LayoutCache layoutCache,
1976:                    long companyId, long groupId, Element rolesEl)
1977:                    throws PortalException, SystemException {
1978:
1979:                String resourceName = Layout.class.getName();
1980:
1981:                exportGroupRoles(layoutCache, companyId, groupId, resourceName,
1982:                        "community", rolesEl);
1983:
1984:                exportUserRoles(layoutCache, companyId, groupId, resourceName,
1985:                        rolesEl);
1986:
1987:                exportInheritedRoles(layoutCache, companyId, groupId,
1988:                        resourceName, "organization", rolesEl);
1989:
1990:                exportInheritedRoles(layoutCache, companyId, groupId,
1991:                        resourceName, "location", rolesEl);
1992:
1993:                exportInheritedRoles(layoutCache, companyId, groupId,
1994:                        resourceName, "user-group", rolesEl);
1995:            }
1996:
1997:            protected void exportPortletData(PortletDataContext context,
1998:                    Layout layout, LayoutTypePortlet layoutTypePortlet,
1999:                    Element parentEl) throws PortalException, SystemException {
2000:
2001:                Iterator itr = portletPreferencesLocalService
2002:                        .getPortletPreferences(layout.getPlid()).iterator();
2003:
2004:                while (itr.hasNext()) {
2005:                    PortletPreferences portletPreferences = (PortletPreferences) itr
2006:                            .next();
2007:
2008:                    javax.portlet.PortletPreferences jxPrefs = portletPreferencesLocalService
2009:                            .getPreferences(layout.getCompanyId(),
2010:                                    portletPreferences.getOwnerId(),
2011:                                    portletPreferences.getOwnerType(),
2012:                                    portletPreferences.getPlid(),
2013:                                    portletPreferences.getPortletId());
2014:
2015:                    String portletId = portletPreferences.getPortletId();
2016:
2017:                    if (layoutTypePortlet.hasPortletId(portletId)) {
2018:                        exportPortletData(context, portletId, jxPrefs, parentEl);
2019:                    }
2020:                }
2021:            }
2022:
2023:            protected void exportPortletData(PortletDataContext context,
2024:                    String portletId,
2025:                    javax.portlet.PortletPreferences portletPreferences,
2026:                    Element parentEl) throws PortalException, SystemException {
2027:
2028:                Portlet portlet = portletLocalService.getPortletById(context
2029:                        .getCompanyId(), portletId);
2030:
2031:                if (portlet == null) {
2032:                    if (_log.isDebugEnabled()) {
2033:                        _log.debug("Do not export portlet data for "
2034:                                + portletId
2035:                                + " because the portlet does not exist");
2036:                    }
2037:
2038:                    return;
2039:                }
2040:
2041:                PortletDataHandler portletDataHandler = portlet
2042:                        .getPortletDataHandlerInstance();
2043:
2044:                if (portletDataHandler == null) {
2045:                    return;
2046:                }
2047:
2048:                if (_log.isDebugEnabled()) {
2049:                    _log.debug("Exporting data for " + portletId);
2050:                }
2051:
2052:                Map parameterMap = context.getParameterMap();
2053:
2054:                boolean exportData = false;
2055:
2056:                if (MapUtil.getBoolean(parameterMap,
2057:                        PortletDataHandlerKeys.PORTLET_DATA + "_" + portletId)
2058:                        || MapUtil.getBoolean(parameterMap,
2059:                                PortletDataHandlerKeys.PORTLET_DATA_ALL)) {
2060:
2061:                    exportData = true;
2062:                }
2063:
2064:                if (!exportData) {
2065:                    if (_log.isDebugEnabled()) {
2066:                        _log.debug("Not exporting data for " + portletId
2067:                                + " because it was not selected by the user");
2068:                    }
2069:
2070:                    return;
2071:                }
2072:
2073:                String data = portletDataHandler.exportData(context, portletId,
2074:                        portletPreferences);
2075:
2076:                if (data == null) {
2077:                    if (_log.isDebugEnabled()) {
2078:                        _log.debug("Not exporting data for " + portletId
2079:                                + " because null data was returned");
2080:                    }
2081:
2082:                    return;
2083:                }
2084:
2085:                Element el = parentEl.addElement("portlet-data");
2086:
2087:                el.addAttribute("portlet-id", portletId);
2088:                el.addCDATA(data);
2089:            }
2090:
2091:            protected void exportPortletPermissions(LayoutCache layoutCache,
2092:                    long companyId, long groupId, Group guestGroup,
2093:                    Layout layout, LayoutTypePortlet layoutTypePortlet,
2094:                    Set portletIds, Element permissionsEl,
2095:                    boolean exportUserPermissions) throws PortalException,
2096:                    SystemException {
2097:
2098:                Iterator itr = layoutTypePortlet.getPortletIds().iterator();
2099:
2100:                while (itr.hasNext()) {
2101:                    String portletId = (String) itr.next();
2102:
2103:                    if (!portletIds.contains(portletId)) {
2104:                        portletIds.add(portletId);
2105:                    }
2106:
2107:                    if (layoutTypePortlet.hasPortletId(portletId)) {
2108:                        String resourceName = PortletImpl
2109:                                .getRootPortletId(portletId);
2110:                        String resourcePrimKey = PortletPermissionUtil
2111:                                .getPrimaryKey(layout.getPlid(), portletId);
2112:
2113:                        Element portletEl = permissionsEl.addElement("portlet");
2114:
2115:                        portletEl.addAttribute("portlet-id", portletId);
2116:
2117:                        exportGroupPermissions(companyId, groupId,
2118:                                resourceName, resourcePrimKey, portletEl,
2119:                                "community-actions");
2120:
2121:                        if (groupId != guestGroup.getGroupId()) {
2122:                            exportGroupPermissions(companyId, guestGroup
2123:                                    .getGroupId(), resourceName,
2124:                                    resourcePrimKey, portletEl, "guest-actions");
2125:                        }
2126:
2127:                        if (exportUserPermissions) {
2128:                            exportUserPermissions(layoutCache, companyId,
2129:                                    groupId, resourceName, resourcePrimKey,
2130:                                    portletEl);
2131:                        }
2132:
2133:                        exportInheritedPermissions(layoutCache, companyId,
2134:                                resourceName, resourcePrimKey, portletEl,
2135:                                "organization");
2136:
2137:                        exportInheritedPermissions(layoutCache, companyId,
2138:                                resourceName, resourcePrimKey, portletEl,
2139:                                "location");
2140:
2141:                        exportInheritedPermissions(layoutCache, companyId,
2142:                                resourceName, resourcePrimKey, portletEl,
2143:                                "user-group");
2144:                    }
2145:                }
2146:            }
2147:
2148:            protected void exportPortletPreferences(long ownerId,
2149:                    int ownerType, long plid, boolean defaultUser,
2150:                    Element parentEl) throws PortalException, SystemException {
2151:
2152:                exportPortletPreferences(ownerId, ownerType, defaultUser, plid,
2153:                        null, parentEl);
2154:            }
2155:
2156:            protected void exportPortletPreferences(long ownerId,
2157:                    int ownerType, boolean defaultUser, long plid,
2158:                    LayoutTypePortlet layoutTypePortlet, Element parentEl)
2159:                    throws PortalException, SystemException {
2160:
2161:                Iterator itr = portletPreferencesLocalService
2162:                        .getPortletPreferences(ownerId, ownerType, plid)
2163:                        .iterator();
2164:
2165:                while (itr.hasNext()) {
2166:                    PortletPreferences portletPreferences = (PortletPreferences) itr
2167:                            .next();
2168:
2169:                    String portletId = portletPreferences.getPortletId();
2170:
2171:                    if ((layoutTypePortlet == null)
2172:                            || (layoutTypePortlet.hasPortletId(portletId))) {
2173:
2174:                        Element el = parentEl.addElement("portlet-preferences");
2175:
2176:                        el.addAttribute("owner-id", String.valueOf(ownerId));
2177:                        el
2178:                                .addAttribute("owner-type", String
2179:                                        .valueOf(ownerType));
2180:                        el.addAttribute("default-user", String
2181:                                .valueOf(defaultUser));
2182:                        el.addAttribute("plid", String.valueOf(plid));
2183:                        el.addAttribute("portlet-id", portletId);
2184:                        el.addElement("preferences").addCDATA(
2185:                                portletPreferences.getPreferences());
2186:                    }
2187:                }
2188:            }
2189:
2190:            protected void exportPortletPreferences(long ownerId,
2191:                    int ownerType, boolean defaultUser, Layout layout,
2192:                    String portletId, Element parentEl) throws PortalException,
2193:                    SystemException {
2194:
2195:                PortletPreferences portletPreferences = null;
2196:
2197:                long plid = layout.getPlid();
2198:
2199:                if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY)
2200:                        || (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP)) {
2201:
2202:                    plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
2203:                }
2204:
2205:                try {
2206:                    portletPreferences = portletPreferencesLocalService
2207:                            .getPortletPreferences(ownerId, ownerType, plid,
2208:                                    portletId);
2209:
2210:                    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
2211:                            .getLayoutType();
2212:
2213:                    if ((layoutTypePortlet == null)
2214:                            || (layoutTypePortlet.hasPortletId(portletId))) {
2215:
2216:                        Element el = parentEl.addElement("portlet-preferences");
2217:
2218:                        el.addAttribute("owner-id", String.valueOf(ownerId));
2219:                        el
2220:                                .addAttribute("owner-type", String
2221:                                        .valueOf(ownerType));
2222:                        el.addAttribute("default-user", String
2223:                                .valueOf(defaultUser));
2224:                        el.addAttribute("plid", String.valueOf(plid));
2225:                        el.addAttribute("portlet-id", portletId);
2226:                        el.addElement("preferences").addCDATA(
2227:                                portletPreferences.getPreferences());
2228:                    }
2229:                } catch (NoSuchPortletPreferencesException nsppe) {
2230:                }
2231:            }
2232:
2233:            protected void exportPortletRoles(LayoutCache layoutCache,
2234:                    long companyId, long groupId, Set portletIds,
2235:                    Element rolesEl) throws PortalException, SystemException {
2236:
2237:                Iterator itr = portletIds.iterator();
2238:
2239:                while (itr.hasNext()) {
2240:                    String portletId = (String) itr.next();
2241:
2242:                    String resourceName = PortletImpl
2243:                            .getRootPortletId(portletId);
2244:
2245:                    Element portletEl = rolesEl.addElement("portlet");
2246:
2247:                    portletEl.addAttribute("portlet-id", portletId);
2248:
2249:                    exportGroupRoles(layoutCache, companyId, groupId,
2250:                            resourceName, "community", portletEl);
2251:
2252:                    exportUserRoles(layoutCache, companyId, groupId,
2253:                            resourceName, portletEl);
2254:
2255:                    exportInheritedRoles(layoutCache, companyId, groupId,
2256:                            resourceName, "organization", portletEl);
2257:
2258:                    exportInheritedRoles(layoutCache, companyId, groupId,
2259:                            resourceName, "location", portletEl);
2260:
2261:                    exportInheritedRoles(layoutCache, companyId, groupId,
2262:                            resourceName, "user-group", portletEl);
2263:
2264:                    if (portletEl.elements().isEmpty()) {
2265:                        rolesEl.remove(portletEl);
2266:                    }
2267:                }
2268:            }
2269:
2270:            protected Element exportRoles(long companyId, String resourceName,
2271:                    int scope, String resourcePrimKey, Element parentEl,
2272:                    String elName, List roles) throws PortalException,
2273:                    SystemException {
2274:
2275:                Element el = parentEl.addElement(elName);
2276:
2277:                Map resourceRoles = roleLocalService.getResourceRoles(
2278:                        companyId, resourceName, scope, resourcePrimKey);
2279:
2280:                Iterator itr = resourceRoles.entrySet().iterator();
2281:
2282:                while (itr.hasNext()) {
2283:                    Map.Entry entry = (Map.Entry) itr.next();
2284:
2285:                    String roleName = entry.getKey().toString();
2286:
2287:                    if (hasRole(roles, roleName)) {
2288:                        Element roleEl = el.addElement("role");
2289:
2290:                        roleEl.addAttribute("name", roleName);
2291:
2292:                        List actions = (List) entry.getValue();
2293:
2294:                        for (int i = 0; i < actions.size(); i++) {
2295:                            String action = (String) actions.get(i);
2296:
2297:                            Element actionKeyEl = roleEl
2298:                                    .addElement("action-key");
2299:
2300:                            actionKeyEl.addText(action);
2301:                            actionKeyEl.addAttribute("scope", String
2302:                                    .valueOf(scope));
2303:                        }
2304:                    }
2305:                }
2306:
2307:                return el;
2308:            }
2309:
2310:            protected void exportRatings(PortletDataContext context,
2311:                    Element root) throws SystemException {
2312:
2313:                try {
2314:                    XStream xStream = new XStream();
2315:
2316:                    Map ratingsEntriesMap = context.getRatingsEntries();
2317:
2318:                    Iterator itr = ratingsEntriesMap.keySet().iterator();
2319:
2320:                    while (itr.hasNext()) {
2321:                        String key = (String) itr.next();
2322:
2323:                        String[] ratingsEntry = key.split(StringPool.POUND);
2324:
2325:                        Element el = root.addElement("ratings");
2326:
2327:                        el.addAttribute("class-name", ratingsEntry[0]);
2328:                        el.addAttribute("class-pk", ratingsEntry[1]);
2329:
2330:                        List ratingsEntries = (List) ratingsEntriesMap.get(key);
2331:
2332:                        String xml = xStream.toXML(ratingsEntries);
2333:
2334:                        Document tempDoc = PortalUtil.readDocumentFromXML(xml);
2335:
2336:                        el.content().add(tempDoc.getRootElement().createCopy());
2337:                    }
2338:                } catch (Exception e) {
2339:                    throw new SystemException(e);
2340:                }
2341:            }
2342:
2343:            protected void exportTags(PortletDataContext context, Element root) {
2344:                Map tagsEntries = context.getTagsEntries();
2345:
2346:                Iterator itr = tagsEntries.keySet().iterator();
2347:
2348:                while (itr.hasNext()) {
2349:                    String key = (String) itr.next();
2350:
2351:                    String[] tagsEntry = key.split(StringPool.POUND);
2352:
2353:                    Element el = root.addElement("tags");
2354:
2355:                    el.addAttribute("class-name", tagsEntry[0]);
2356:                    el.addAttribute("class-pk", tagsEntry[1]);
2357:                    el.addAttribute("entries", StringUtil.merge(
2358:                            (String[]) tagsEntries.get(key), ","));
2359:                }
2360:            }
2361:
2362:            protected byte[] exportTheme(LayoutSet layoutSet)
2363:                    throws IOException, PortalException, SystemException {
2364:
2365:                Theme theme = layoutSet.getTheme();
2366:
2367:                ZipWriter zipWriter = new ZipWriter();
2368:
2369:                String lookAndFeelXML = ContentUtil
2370:                        .get("com/liferay/portal/dependencies/liferay-look-and-feel.xml.tmpl");
2371:
2372:                lookAndFeelXML = StringUtil.replace(lookAndFeelXML,
2373:                        new String[] { "[$TEMPLATE_EXTENSION$]",
2374:                                "[$VIRTUAL_PATH$]" }, new String[] {
2375:                                theme.getTemplateExtension(),
2376:                                theme.getVirtualPath() });
2377:
2378:                zipWriter.addEntry("liferay-look-and-feel.xml", lookAndFeelXML);
2379:
2380:                String servletContextName = theme.getServletContextName();
2381:
2382:                ServletContext ctx = VelocityContextPool
2383:                        .get(servletContextName);
2384:
2385:                if (ctx == null) {
2386:                    if (_log.isWarnEnabled()) {
2387:                        _log.warn("Servlet context not found for theme "
2388:                                + theme.getThemeId());
2389:                    }
2390:
2391:                    return null;
2392:                }
2393:
2394:                File cssPath = null;
2395:                File imagesPath = null;
2396:                File javaScriptPath = null;
2397:                File templatesPath = null;
2398:
2399:                if (!theme.isLoadFromServletContext()) {
2400:                    ThemeLoader themeLoader = ThemeLoaderFactory
2401:                            .getThemeLoader(servletContextName);
2402:
2403:                    if (themeLoader == null) {
2404:                        _log.error(servletContextName
2405:                                + " does not map to a theme loader");
2406:                    } else {
2407:                        String realPath = themeLoader.getFileStorage()
2408:                                .getPath()
2409:                                + "/" + theme.getName();
2410:
2411:                        cssPath = new File(realPath + "/css");
2412:                        imagesPath = new File(realPath + "/images");
2413:                        javaScriptPath = new File(realPath + "/javascript");
2414:                        templatesPath = new File(realPath + "/templates");
2415:                    }
2416:                } else {
2417:                    cssPath = new File(ctx.getRealPath(theme.getCssPath()));
2418:                    imagesPath = new File(ctx
2419:                            .getRealPath(theme.getImagesPath()));
2420:                    javaScriptPath = new File(ctx.getRealPath(theme
2421:                            .getJavaScriptPath()));
2422:                    templatesPath = new File(ctx.getRealPath(theme
2423:                            .getTemplatesPath()));
2424:                }
2425:
2426:                exportThemeFiles("css", cssPath, zipWriter);
2427:                exportThemeFiles("images", imagesPath, zipWriter);
2428:                exportThemeFiles("javascript", javaScriptPath, zipWriter);
2429:                exportThemeFiles("templates", templatesPath, zipWriter);
2430:
2431:                return zipWriter.finish();
2432:            }
2433:
2434:            protected void exportThemeFiles(String path, File dir,
2435:                    ZipWriter zipWriter) throws IOException {
2436:
2437:                if ((dir == null) || (!dir.exists())) {
2438:                    return;
2439:                }
2440:
2441:                File[] files = dir.listFiles();
2442:
2443:                for (int i = 0; i < files.length; i++) {
2444:                    File file = files[i];
2445:
2446:                    if (file.isDirectory()) {
2447:                        exportThemeFiles(path + "/" + file.getName(), file,
2448:                                zipWriter);
2449:                    } else {
2450:                        zipWriter.addEntry(path + "/" + file.getName(),
2451:                                FileUtil.getBytes(file));
2452:                    }
2453:                }
2454:            }
2455:
2456:            protected void exportUserPermissions(LayoutCache layoutCache,
2457:                    long companyId, long groupId, String resourceName,
2458:                    String resourcePrimKey, Element parentEl)
2459:                    throws PortalException, SystemException {
2460:
2461:                StopWatch stopWatch = null;
2462:
2463:                if (_log.isDebugEnabled()) {
2464:                    stopWatch = new StopWatch();
2465:
2466:                    stopWatch.start();
2467:                }
2468:
2469:                Element userPermissionsEl = DocumentHelper
2470:                        .createElement("user-permissions");
2471:
2472:                List users = layoutCache.getGroupUsers(groupId);
2473:
2474:                for (int i = 0; i < users.size(); i++) {
2475:                    User user = (User) users.get(i);
2476:
2477:                    String emailAddress = user.getEmailAddress();
2478:
2479:                    Element userActionsEl = DocumentHelper
2480:                            .createElement("user-actions");
2481:
2482:                    List permissions = permissionLocalService
2483:                            .getUserPermissions(user.getUserId(), companyId,
2484:                                    resourceName,
2485:                                    ResourceImpl.SCOPE_INDIVIDUAL,
2486:                                    resourcePrimKey);
2487:
2488:                    List actions = ResourceActionsUtil.getActions(permissions);
2489:
2490:                    for (int j = 0; j < actions.size(); j++) {
2491:                        String action = (String) actions.get(j);
2492:
2493:                        Element actionKeyEl = userActionsEl
2494:                                .addElement("action-key");
2495:
2496:                        actionKeyEl.addText(action);
2497:                    }
2498:
2499:                    if (!userActionsEl.elements().isEmpty()) {
2500:                        userActionsEl.addAttribute("email-address",
2501:                                emailAddress);
2502:                        userPermissionsEl.add(userActionsEl);
2503:                    }
2504:                }
2505:
2506:                if (!userPermissionsEl.elements().isEmpty()) {
2507:                    parentEl.add(userPermissionsEl);
2508:                }
2509:
2510:                if (_log.isDebugEnabled()) {
2511:                    _log.debug("Export user permissions for {" + resourceName
2512:                            + ", " + resourcePrimKey + "} with " + users.size()
2513:                            + " users takes " + stopWatch.getTime() + " ms");
2514:                }
2515:            }
2516:
2517:            protected void exportUserRoles(LayoutCache layoutCache,
2518:                    long companyId, long groupId, String resourceName,
2519:                    Element parentEl) throws PortalException, SystemException {
2520:
2521:                Element userRolesEl = DocumentHelper
2522:                        .createElement("user-roles");
2523:
2524:                List users = layoutCache.getGroupUsers(groupId);
2525:
2526:                for (int i = 0; i < users.size(); i++) {
2527:                    User user = (User) users.get(i);
2528:
2529:                    long userId = user.getUserId();
2530:                    String emailAddress = user.getEmailAddress();
2531:
2532:                    List userRoles = layoutCache.getUserRoles(userId);
2533:
2534:                    Element userEl = exportRoles(companyId, resourceName,
2535:                            ResourceImpl.SCOPE_GROUP, String.valueOf(groupId),
2536:                            userRolesEl, "user", userRoles);
2537:
2538:                    if (userEl.elements().isEmpty()) {
2539:                        userRolesEl.remove(userEl);
2540:                    } else {
2541:                        userEl.addAttribute("email-address", emailAddress);
2542:                    }
2543:                }
2544:
2545:                if (!userRolesEl.elements().isEmpty()) {
2546:                    parentEl.add(userRolesEl);
2547:                }
2548:            }
2549:
2550:            protected void fixTypeSettings(Layout layout) {
2551:                if (layout.getType().equals(LayoutImpl.TYPE_URL)) {
2552:                    Properties typeSettings = layout
2553:                            .getTypeSettingsProperties();
2554:
2555:                    String url = GetterUtil.getString(typeSettings
2556:                            .getProperty("url"));
2557:
2558:                    String friendlyURLPrivateGroupPath = PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
2559:                    String friendlyURLPrivateUserPath = PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
2560:                    String friendlyURLPublicPath = PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
2561:
2562:                    if (url.startsWith(friendlyURLPrivateGroupPath)
2563:                            || url.startsWith(friendlyURLPrivateUserPath)
2564:                            || url.startsWith(friendlyURLPublicPath)) {
2565:
2566:                        int x = url.indexOf(StringPool.SLASH, 1);
2567:
2568:                        if (x > 0) {
2569:                            int y = url.indexOf(StringPool.SLASH, x + 1);
2570:
2571:                            if (y > x) {
2572:                                String fixedUrl = url.substring(0, x) +
2573:
2574:                                layout.getGroup().getFriendlyURL() +
2575:
2576:                                url.substring(y);
2577:
2578:                                typeSettings.setProperty("url", fixedUrl);
2579:                            }
2580:                        }
2581:                    }
2582:                }
2583:            }
2584:
2585:            protected List getActions(Element el) {
2586:                List actions = new ArrayList();
2587:
2588:                Iterator itr = el.elements("action-key").iterator();
2589:
2590:                while (itr.hasNext()) {
2591:                    Element actionEl = (Element) itr.next();
2592:
2593:                    actions.add(actionEl.getText());
2594:                }
2595:
2596:                return actions;
2597:            }
2598:
2599:            protected String getFriendlyURL(String friendlyURL) {
2600:                return Normalizer.normalizeToAscii(friendlyURL.trim()
2601:                        .toLowerCase());
2602:            }
2603:
2604:            protected long getNextLayoutId(long groupId, boolean privateLayout)
2605:                    throws SystemException {
2606:
2607:                long layoutId = 0;
2608:
2609:                List layouts = layoutPersistence.findByG_P(groupId,
2610:                        privateLayout);
2611:
2612:                for (int i = 0; i < layouts.size(); i++) {
2613:                    Layout curLayout = (Layout) layouts.get(i);
2614:
2615:                    long curLayoutId = curLayout.getLayoutId();
2616:
2617:                    if (curLayoutId > layoutId) {
2618:                        layoutId = curLayoutId;
2619:                    }
2620:                }
2621:
2622:                return ++layoutId;
2623:            }
2624:
2625:            protected int getNextPriority(long groupId, boolean privateLayout,
2626:                    long parentLayoutId) throws SystemException {
2627:
2628:                List layouts = layoutPersistence.findByG_P_P(groupId,
2629:                        privateLayout, parentLayoutId);
2630:
2631:                if (layouts.size() == 0) {
2632:                    return 0;
2633:                }
2634:
2635:                Layout layout = (Layout) layouts.get(layouts.size() - 1);
2636:
2637:                return layout.getPriority() + 1;
2638:            }
2639:
2640:            protected long getParentLayoutId(long groupId,
2641:                    boolean privateLayout, long parentLayoutId)
2642:                    throws PortalException, SystemException {
2643:
2644:                if (parentLayoutId != LayoutImpl.DEFAULT_PARENT_LAYOUT_ID) {
2645:
2646:                    // Ensure parent layout exists
2647:
2648:                    try {
2649:                        layoutPersistence.findByG_P_L(groupId, privateLayout,
2650:                                parentLayoutId);
2651:                    } catch (NoSuchLayoutException nsfe) {
2652:                        parentLayoutId = LayoutImpl.DEFAULT_PARENT_LAYOUT_ID;
2653:                    }
2654:                }
2655:
2656:                return parentLayoutId;
2657:            }
2658:
2659:            protected boolean hasRole(List roles, String roleName) {
2660:                if ((roles == null) || (roles.size() == 0)) {
2661:                    return false;
2662:                }
2663:
2664:                for (int i = 0; i < roles.size(); i++) {
2665:                    Role role = (Role) roles.get(i);
2666:
2667:                    if (role.getName().equals(roleName)) {
2668:                        return true;
2669:                    }
2670:                }
2671:
2672:                return false;
2673:            }
2674:
2675:            protected void importGroupPermissions(LayoutCache layoutCache,
2676:                    long companyId, long groupId, String resourceName,
2677:                    String resourcePrimKey, Element parentEl, String elName,
2678:                    boolean portletActions) throws PortalException,
2679:                    SystemException {
2680:
2681:                Element actionEl = parentEl.element(elName);
2682:
2683:                if (actionEl == null) {
2684:                    return;
2685:                }
2686:
2687:                List actions = getActions(actionEl);
2688:
2689:                Resource resource = layoutCache.getResource(companyId, groupId,
2690:                        resourceName, ResourceImpl.SCOPE_INDIVIDUAL,
2691:                        resourcePrimKey, portletActions);
2692:
2693:                permissionLocalService.setGroupPermissions(groupId,
2694:                        (String[]) actions.toArray(new String[0]), resource
2695:                                .getResourceId());
2696:            }
2697:
2698:            protected void importGroupRoles(LayoutCache layoutCache,
2699:                    long companyId, long groupId, String resourceName,
2700:                    String entityName, Element parentEl)
2701:                    throws PortalException, SystemException {
2702:
2703:                Element entityRolesEl = parentEl.element(entityName + "-roles");
2704:
2705:                if (entityRolesEl == null) {
2706:                    return;
2707:                }
2708:
2709:                importRolePermissions(layoutCache, companyId, resourceName,
2710:                        ResourceImpl.SCOPE_GROUP, String.valueOf(groupId),
2711:                        entityRolesEl, true);
2712:            }
2713:
2714:            protected void importInheritedPermissions(LayoutCache layoutCache,
2715:                    long companyId, String resourceName,
2716:                    String resourcePrimKey, Element permissionsEl,
2717:                    String entityName, boolean portletActions)
2718:                    throws PortalException, SystemException {
2719:
2720:                Element entityPermissionsEl = permissionsEl.element(entityName
2721:                        + "-permissions");
2722:
2723:                if (entityPermissionsEl == null) {
2724:                    return;
2725:                }
2726:
2727:                List actionsEls = entityPermissionsEl.elements(entityName
2728:                        + "-actions");
2729:
2730:                for (int i = 0; i < actionsEls.size(); i++) {
2731:                    Element actionEl = (Element) actionsEls.get(i);
2732:
2733:                    String name = actionEl.attributeValue("name");
2734:
2735:                    long entityGroupId = layoutCache.getEntityGroupId(
2736:                            companyId, entityName, name);
2737:
2738:                    if (entityGroupId == 0) {
2739:                        _log.warn("Ignore inherited permissions for entity "
2740:                                + entityName + " with name " + name);
2741:                    } else {
2742:                        Element parentEl = DocumentHelper
2743:                                .createElement("parent");
2744:
2745:                        parentEl.add(actionEl.createCopy());
2746:
2747:                        importGroupPermissions(layoutCache, companyId,
2748:                                entityGroupId, resourceName, resourcePrimKey,
2749:                                parentEl, entityName + "-actions",
2750:                                portletActions);
2751:                    }
2752:                }
2753:            }
2754:
2755:            protected void importInheritedRoles(LayoutCache layoutCache,
2756:                    long companyId, long groupId, String resourceName,
2757:                    String entityName, Element parentEl)
2758:                    throws PortalException, SystemException {
2759:
2760:                Element entityRolesEl = parentEl.element(entityName + "-roles");
2761:
2762:                if (entityRolesEl == null) {
2763:                    return;
2764:                }
2765:
2766:                List entityEls = entityRolesEl.elements(entityName);
2767:
2768:                for (int i = 0; i < entityEls.size(); i++) {
2769:                    Element entityEl = (Element) entityEls.get(i);
2770:
2771:                    String name = entityEl.attributeValue("name");
2772:
2773:                    long entityGroupId = layoutCache.getEntityGroupId(
2774:                            companyId, entityName, name);
2775:
2776:                    if (entityGroupId == 0) {
2777:                        _log.warn("Ignore inherited roles for entity "
2778:                                + entityName + " with name " + name);
2779:                    } else {
2780:                        importRolePermissions(layoutCache, companyId,
2781:                                resourceName, ResourceImpl.SCOPE_GROUP, String
2782:                                        .valueOf(groupId), entityEl, false);
2783:                    }
2784:                }
2785:            }
2786:
2787:            protected void importLayoutPermissions(LayoutCache layoutCache,
2788:                    long companyId, long groupId, Group guestGroup,
2789:                    Layout layout, Element permissionsEl,
2790:                    boolean importUserPermissions) throws PortalException,
2791:                    SystemException {
2792:
2793:                String resourceName = Layout.class.getName();
2794:                String resourcePrimKey = String.valueOf(layout.getPlid());
2795:
2796:                importGroupPermissions(layoutCache, companyId, groupId,
2797:                        resourceName, resourcePrimKey, permissionsEl,
2798:                        "community-actions", false);
2799:
2800:                if (groupId != guestGroup.getGroupId()) {
2801:                    importGroupPermissions(layoutCache, companyId, guestGroup
2802:                            .getGroupId(), resourceName, resourcePrimKey,
2803:                            permissionsEl, "guest-actions", false);
2804:                }
2805:
2806:                if (importUserPermissions) {
2807:                    importUserPermissions(layoutCache, companyId, groupId,
2808:                            resourceName, resourcePrimKey, permissionsEl, false);
2809:                }
2810:
2811:                importInheritedPermissions(layoutCache, companyId,
2812:                        resourceName, resourcePrimKey, permissionsEl,
2813:                        "organization", false);
2814:
2815:                importInheritedPermissions(layoutCache, companyId,
2816:                        resourceName, resourcePrimKey, permissionsEl,
2817:                        "location", false);
2818:
2819:                importInheritedPermissions(layoutCache, companyId,
2820:                        resourceName, resourcePrimKey, permissionsEl,
2821:                        "user-group", false);
2822:            }
2823:
2824:            protected void importLayoutRoles(LayoutCache layoutCache,
2825:                    long companyId, long groupId, Element rolesEl)
2826:                    throws PortalException, SystemException {
2827:
2828:                String resourceName = Layout.class.getName();
2829:
2830:                importGroupRoles(layoutCache, companyId, groupId, resourceName,
2831:                        "community", rolesEl);
2832:
2833:                importUserRoles(layoutCache, companyId, groupId, resourceName,
2834:                        rolesEl);
2835:
2836:                importInheritedRoles(layoutCache, companyId, groupId,
2837:                        resourceName, "organization", rolesEl);
2838:
2839:                importInheritedRoles(layoutCache, companyId, groupId,
2840:                        resourceName, "location", rolesEl);
2841:
2842:                importInheritedRoles(layoutCache, companyId, groupId,
2843:                        resourceName, "user-group", rolesEl);
2844:            }
2845:
2846:            protected void importPortletData(PortletDataContext context,
2847:                    Layout layout, Element parentEl) throws PortalException,
2848:                    SystemException {
2849:
2850:                Iterator itr = parentEl.elements("portlet-data").iterator();
2851:
2852:                while (itr.hasNext()) {
2853:                    Element el = (Element) itr.next();
2854:
2855:                    String portletId = el.attributeValue("portlet-id");
2856:
2857:                    try {
2858:                        PortletPreferences portletPreferences = portletPreferencesPersistence
2859:                                .findByO_O_P_P(
2860:                                        PortletKeys.PREFS_OWNER_ID_DEFAULT,
2861:                                        PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
2862:                                        layout.getPlid(), portletId);
2863:
2864:                        String preferences = importPortletData(context,
2865:                                portletId, portletPreferences, el);
2866:
2867:                        if (preferences != null) {
2868:                            portletPreferences.setPreferences(preferences);
2869:
2870:                            portletPreferencesPersistence
2871:                                    .update(portletPreferences);
2872:                        }
2873:                    } catch (NoSuchPortletPreferencesException nsppe) {
2874:                    }
2875:                }
2876:            }
2877:
2878:            protected String importPortletData(PortletDataContext context,
2879:                    String portletId, PortletPreferences portletPreferences,
2880:                    Element parentEl) throws PortalException, SystemException {
2881:
2882:                Portlet portlet = portletLocalService.getPortletById(context
2883:                        .getCompanyId(), portletId);
2884:
2885:                if (portlet == null) {
2886:                    if (_log.isDebugEnabled()) {
2887:                        _log.debug("Do not import portlet data for "
2888:                                + portletId
2889:                                + " because the portlet does not exist");
2890:                    }
2891:
2892:                    return null;
2893:                }
2894:
2895:                PortletDataHandler portletDataHandler = portlet
2896:                        .getPortletDataHandlerInstance();
2897:
2898:                if (portletDataHandler == null) {
2899:                    if (_log.isDebugEnabled()) {
2900:                        _log.debug("Do not import portlet data for "
2901:                                + portletId
2902:                                + " because the portlet does not have a "
2903:                                + "PortletDataHandler");
2904:                    }
2905:
2906:                    return null;
2907:                }
2908:
2909:                if (_log.isDebugEnabled()) {
2910:                    _log.debug("Importing data for " + portletId);
2911:                }
2912:
2913:                PortletPreferencesImpl prefsImpl = (PortletPreferencesImpl) PortletPreferencesSerializer
2914:                        .fromDefaultXML(portletPreferences.getPreferences());
2915:
2916:                prefsImpl = (PortletPreferencesImpl) portletDataHandler
2917:                        .importData(context, portletId, prefsImpl, parentEl
2918:                                .getText());
2919:
2920:                if (prefsImpl == null) {
2921:                    return null;
2922:                }
2923:
2924:                return PortletPreferencesSerializer.toXML(prefsImpl);
2925:            }
2926:
2927:            protected void importPortletPermissions(LayoutCache layoutCache,
2928:                    long companyId, long groupId, Group guestGroup,
2929:                    Layout layout, Element permissionsEl,
2930:                    boolean importUserPermissions) throws PortalException,
2931:                    SystemException {
2932:
2933:                Iterator itr = permissionsEl.elements("portlet").iterator();
2934:
2935:                while (itr.hasNext()) {
2936:                    Element portletEl = (Element) itr.next();
2937:
2938:                    String portletId = portletEl.attributeValue("portlet-id");
2939:
2940:                    String resourceName = PortletImpl
2941:                            .getRootPortletId(portletId);
2942:                    String resourcePrimKey = PortletPermissionUtil
2943:                            .getPrimaryKey(layout.getPlid(), portletId);
2944:
2945:                    Portlet portlet = portletLocalService.getPortletById(
2946:                            companyId, resourceName);
2947:
2948:                    if (portlet == null) {
2949:                        if (_log.isDebugEnabled()) {
2950:                            _log.debug("Do not import portlet permissions for "
2951:                                    + portletId
2952:                                    + " because the portlet does not exist");
2953:                        }
2954:                    } else {
2955:                        importGroupPermissions(layoutCache, companyId, groupId,
2956:                                resourceName, resourcePrimKey, portletEl,
2957:                                "community-actions", true);
2958:
2959:                        if (groupId != guestGroup.getGroupId()) {
2960:                            importGroupPermissions(layoutCache, companyId,
2961:                                    guestGroup.getGroupId(), resourceName,
2962:                                    resourcePrimKey, portletEl,
2963:                                    "guest-actions", true);
2964:                        }
2965:
2966:                        if (importUserPermissions) {
2967:                            importUserPermissions(layoutCache, companyId,
2968:                                    groupId, resourceName, resourcePrimKey,
2969:                                    portletEl, true);
2970:                        }
2971:
2972:                        importInheritedPermissions(layoutCache, companyId,
2973:                                resourceName, resourcePrimKey, portletEl,
2974:                                "organization", true);
2975:
2976:                        importInheritedPermissions(layoutCache, companyId,
2977:                                resourceName, resourcePrimKey, portletEl,
2978:                                "location", true);
2979:
2980:                        importInheritedPermissions(layoutCache, companyId,
2981:                                resourceName, resourcePrimKey, portletEl,
2982:                                "user-group", true);
2983:                    }
2984:                }
2985:            }
2986:
2987:            protected void importPortletPreferences(long companyId, long plid,
2988:                    Element parentEl, boolean importPortletSetup,
2989:                    boolean importUserPreferences) throws PortalException,
2990:                    SystemException {
2991:
2992:                long defaultUserId = userLocalService
2993:                        .getDefaultUserId(companyId);
2994:
2995:                Iterator itr = parentEl.elements("portlet-preferences")
2996:                        .iterator();
2997:
2998:                while (itr.hasNext()) {
2999:                    Element el = (Element) itr.next();
3000:
3001:                    long ownerId = GetterUtil.getLong(el
3002:                            .attributeValue("owner-id"));
3003:                    int ownerType = GetterUtil.getInteger(el
3004:                            .attributeValue("owner-type"));
3005:
3006:                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
3007:                        continue;
3008:                    }
3009:
3010:                    if ((ownerType != PortletKeys.PREFS_OWNER_TYPE_USER)
3011:                            && !importPortletSetup) {
3012:
3013:                        continue;
3014:                    }
3015:
3016:                    if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_USER)
3017:                            && (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT)
3018:                            && !importUserPreferences) {
3019:
3020:                        continue;
3021:                    }
3022:
3023:                    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
3024:                        plid = PortletKeys.PREFS_PLID_SHARED;
3025:                    }
3026:
3027:                    boolean defaultUser = GetterUtil.getBoolean(el
3028:                            .attributeValue("default-user"));
3029:                    String portletId = el.attributeValue("portlet-id");
3030:                    String preferences = el.elementText("preferences");
3031:
3032:                    if (defaultUser) {
3033:                        ownerId = defaultUserId;
3034:                    }
3035:
3036:                    try {
3037:                        portletPreferencesLocalService
3038:                                .deletePortletPreferences(ownerId, ownerType,
3039:                                        plid, portletId);
3040:                    } catch (NoSuchPortletPreferencesException nsppe) {
3041:                    }
3042:
3043:                    long portletPreferencesId = counterLocalService.increment();
3044:
3045:                    PortletPreferences portletPreferences = portletPreferencesPersistence
3046:                            .create(portletPreferencesId);
3047:
3048:                    portletPreferences.setOwnerId(ownerId);
3049:                    portletPreferences.setOwnerType(ownerType);
3050:                    portletPreferences.setPlid(plid);
3051:                    portletPreferences.setPortletId(portletId);
3052:
3053:                    portletPreferences.setPreferences(preferences);
3054:
3055:                    portletPreferencesPersistence.update(portletPreferences);
3056:                }
3057:            }
3058:
3059:            protected void importPortletRoles(LayoutCache layoutCache,
3060:                    long companyId, long groupId, Element rolesEl)
3061:                    throws PortalException, SystemException {
3062:
3063:                Iterator itr = rolesEl.elements("portlet").iterator();
3064:
3065:                while (itr.hasNext()) {
3066:                    Element portletEl = (Element) itr.next();
3067:
3068:                    String portletId = portletEl.attributeValue("portlet-id");
3069:
3070:                    String resourceName = PortletImpl
3071:                            .getRootPortletId(portletId);
3072:
3073:                    Portlet portlet = portletLocalService.getPortletById(
3074:                            companyId, resourceName);
3075:
3076:                    if (portlet == null) {
3077:                        if (_log.isDebugEnabled()) {
3078:                            _log.debug("Do not import portlet roles for "
3079:                                    + portletId
3080:                                    + " because the portlet does not exist");
3081:                        }
3082:                    } else {
3083:                        importGroupRoles(layoutCache, companyId, groupId,
3084:                                resourceName, "community", portletEl);
3085:
3086:                        importUserRoles(layoutCache, companyId, groupId,
3087:                                resourceName, portletEl);
3088:
3089:                        importInheritedRoles(layoutCache, companyId, groupId,
3090:                                resourceName, "organization", portletEl);
3091:
3092:                        importInheritedRoles(layoutCache, companyId, groupId,
3093:                                resourceName, "location", portletEl);
3094:
3095:                        importInheritedRoles(layoutCache, companyId, groupId,
3096:                                resourceName, "user-group", portletEl);
3097:                    }
3098:                }
3099:            }
3100:
3101:            protected void importRolePermissions(LayoutCache layoutCache,
3102:                    long companyId, String resourceName, int scope,
3103:                    String resourcePrimKey, Element parentEl,
3104:                    boolean communityRole) throws PortalException,
3105:                    SystemException {
3106:
3107:                List roleEls = parentEl.elements("role");
3108:
3109:                for (int i = 0; i < roleEls.size(); i++) {
3110:                    Element roleEl = (Element) roleEls.get(i);
3111:
3112:                    String roleName = roleEl.attributeValue("name");
3113:
3114:                    Role role = layoutCache.getRole(companyId, roleName);
3115:
3116:                    if (role == null) {
3117:                        _log.warn("Ignoring permissions for role with name "
3118:                                + roleName);
3119:                    } else {
3120:                        List actions = getActions(roleEl);
3121:
3122:                        permissionLocalService.setRolePermissions(role
3123:                                .getRoleId(), companyId, resourceName, scope,
3124:                                resourcePrimKey, (String[]) actions
3125:                                        .toArray(new String[0]));
3126:
3127:                        if (communityRole) {
3128:                            long[] groupIds = { GetterUtil
3129:                                    .getLong(resourcePrimKey) };
3130:
3131:                            groupLocalService.addRoleGroups(role.getRoleId(),
3132:                                    groupIds);
3133:                        }
3134:                    }
3135:                }
3136:            }
3137:
3138:            protected String importTheme(LayoutSet layoutSet, byte[] themeZip)
3139:                    throws IOException {
3140:
3141:                ThemeLoader themeLoader = ThemeLoaderFactory
3142:                        .getDefaultThemeLoader();
3143:
3144:                if (themeLoader == null) {
3145:                    _log.error("No theme loaders are deployed");
3146:
3147:                    return null;
3148:                }
3149:
3150:                ZipReader zipReader = new ZipReader(new ByteArrayInputStream(
3151:                        themeZip));
3152:
3153:                Map entries = zipReader.getEntries();
3154:
3155:                String lookAndFeelXML = new String((byte[]) entries
3156:                        .get("liferay-look-and-feel.xml"));
3157:
3158:                String themeId = String.valueOf(layoutSet.getGroupId());
3159:
3160:                if (layoutSet.isPrivateLayout()) {
3161:                    themeId += "-private";
3162:                } else {
3163:                    themeId += "-public";
3164:                }
3165:
3166:                if (PropsValues.THEME_LOADER_NEW_THEME_ID_ON_IMPORT) {
3167:                    Date now = new Date();
3168:
3169:                    themeId += "-" + Time.getShortTimestamp(now);
3170:                }
3171:
3172:                String themeName = themeId;
3173:
3174:                lookAndFeelXML = StringUtil.replace(lookAndFeelXML,
3175:                        new String[] { "[$GROUP_ID$]", "[$THEME_ID$]",
3176:                                "[$THEME_NAME$]" }, new String[] {
3177:                                String.valueOf(layoutSet.getGroupId()),
3178:                                themeId, themeName });
3179:
3180:                FileUtil.deltree(themeLoader.getFileStorage() + "/" + themeId);
3181:
3182:                Iterator itr = entries.entrySet().iterator();
3183:
3184:                while (itr.hasNext()) {
3185:                    Map.Entry entry = (Map.Entry) itr.next();
3186:
3187:                    String key = (String) entry.getKey();
3188:                    byte[] value = (byte[]) entry.getValue();
3189:
3190:                    if (key.equals("liferay-look-and-feel.xml")) {
3191:                        value = lookAndFeelXML.getBytes();
3192:                    }
3193:
3194:                    FileUtil.write(themeLoader.getFileStorage() + "/" + themeId
3195:                            + "/" + key, value);
3196:                }
3197:
3198:                themeLoader.loadThemes();
3199:
3200:                CommLink commLink = CommLink.getInstance();
3201:
3202:                MethodWrapper methodWrapper = new MethodWrapper(
3203:                        ThemeLoaderFactory.class.getName(), "loadThemes");
3204:
3205:                commLink.send(methodWrapper);
3206:
3207:                themeId += PortletImpl.WAR_SEPARATOR
3208:                        + themeLoader.getServletContextName();
3209:
3210:                return PortalUtil.getJsSafePortletId(themeId);
3211:            }
3212:
3213:            protected void importUserPermissions(LayoutCache layoutCache,
3214:                    long companyId, long groupId, String resourceName,
3215:                    String resourcePrimKey, Element parentEl,
3216:                    boolean portletActions) throws PortalException,
3217:                    SystemException {
3218:
3219:                Element userPermissionsEl = parentEl
3220:                        .element("user-permissions");
3221:
3222:                if (userPermissionsEl == null) {
3223:                    return;
3224:                }
3225:
3226:                List userActionsEls = userPermissionsEl
3227:                        .elements("user-actions");
3228:
3229:                for (int i = 0; i < userActionsEls.size(); i++) {
3230:                    Element userActionsEl = (Element) userActionsEls.get(i);
3231:
3232:                    String emailAddress = userActionsEl
3233:                            .attributeValue("email-address");
3234:
3235:                    User user = layoutCache.getUser(companyId, groupId,
3236:                            emailAddress);
3237:
3238:                    if (user == null) {
3239:                        if (_log.isWarnEnabled()) {
3240:                            _log
3241:                                    .warn("Ignoring permissions for user with email address "
3242:                                            + emailAddress);
3243:                        }
3244:                    } else {
3245:                        List actions = getActions(userActionsEl);
3246:
3247:                        Resource resource = layoutCache.getResource(companyId,
3248:                                groupId, resourceName,
3249:                                ResourceImpl.SCOPE_INDIVIDUAL, resourcePrimKey,
3250:                                portletActions);
3251:
3252:                        permissionLocalService.setUserPermissions(user
3253:                                .getUserId(), (String[]) actions
3254:                                .toArray(new String[0]), resource
3255:                                .getResourceId());
3256:                    }
3257:                }
3258:            }
3259:
3260:            protected void importUserRoles(LayoutCache layoutCache,
3261:                    long companyId, long groupId, String resourceName,
3262:                    Element parentEl) throws PortalException, SystemException {
3263:
3264:                Element userRolesEl = parentEl.element("user-roles");
3265:
3266:                if (userRolesEl == null) {
3267:                    return;
3268:                }
3269:
3270:                LinkedHashMap params = new LinkedHashMap();
3271:
3272:                params.put("usersGroups", new Long(groupId));
3273:
3274:                List userEls = userRolesEl.elements("user");
3275:
3276:                for (int i = 0; i < userEls.size(); i++) {
3277:                    Element userEl = (Element) userEls.get(i);
3278:
3279:                    String emailAddress = userEl
3280:                            .attributeValue("email-address");
3281:
3282:                    User user = layoutCache.getUser(companyId, groupId,
3283:                            emailAddress);
3284:
3285:                    if (user == null) {
3286:                        if (_log.isWarnEnabled()) {
3287:                            _log
3288:                                    .warn("Ignoring roles for user with email address "
3289:                                            + emailAddress);
3290:                        }
3291:                    } else {
3292:                        importRolePermissions(layoutCache, companyId,
3293:                                resourceName, ResourceImpl.SCOPE_GROUP, String
3294:                                        .valueOf(groupId), userEl, false);
3295:                    }
3296:                }
3297:            }
3298:
3299:            protected boolean isDescendant(Layout layout, long layoutId)
3300:                    throws PortalException, SystemException {
3301:
3302:                if (layout.getLayoutId() == layoutId) {
3303:                    return true;
3304:                } else {
3305:                    Iterator itr = layout.getChildren().iterator();
3306:
3307:                    while (itr.hasNext()) {
3308:                        Layout childLayout = (Layout) itr.next();
3309:
3310:                        if (isDescendant(childLayout, layoutId)) {
3311:                            return true;
3312:                        }
3313:                    }
3314:
3315:                    return false;
3316:                }
3317:            }
3318:
3319:            protected void readComments(PortletDataContext context, Element root)
3320:                    throws PortalException, SystemException {
3321:
3322:                try {
3323:                    XStream xStream = new XStream();
3324:
3325:                    Iterator itr = root.elements("comments").iterator();
3326:
3327:                    while (itr.hasNext()) {
3328:                        Element el = (Element) itr.next();
3329:
3330:                        String className = GetterUtil.getString(el
3331:                                .attributeValue("class-name"));
3332:                        long classPK = GetterUtil.getLong(el
3333:                                .attributeValue("class-pk"));
3334:
3335:                        Element messagesListEl = el.element("list");
3336:
3337:                        Document tempDoc = DocumentHelper.createDocument();
3338:
3339:                        tempDoc.content().add(messagesListEl.createCopy());
3340:
3341:                        List messages = (List) xStream.fromXML(XMLFormatter
3342:                                .toString(tempDoc));
3343:
3344:                        context.addComments(className, new Long(classPK),
3345:                                messages);
3346:                    }
3347:                } catch (Exception e) {
3348:                    throw new SystemException(e);
3349:                }
3350:            }
3351:
3352:            protected void readRatings(PortletDataContext context, Element root)
3353:                    throws PortalException, SystemException {
3354:
3355:                try {
3356:                    XStream xStream = new XStream();
3357:
3358:                    Iterator itr = root.elements("ratings").iterator();
3359:
3360:                    while (itr.hasNext()) {
3361:                        Element el = (Element) itr.next();
3362:
3363:                        String className = GetterUtil.getString(el
3364:                                .attributeValue("class-name"));
3365:                        long classPK = GetterUtil.getLong(el
3366:                                .attributeValue("class-pk"));
3367:
3368:                        Element entriesListEl = el.element("list");
3369:
3370:                        Document tempDoc = DocumentHelper.createDocument();
3371:
3372:                        tempDoc.content().add(entriesListEl.createCopy());
3373:
3374:                        List entries = (List) xStream.fromXML(XMLFormatter
3375:                                .toString(tempDoc));
3376:
3377:                        context.addRatingsEntries(className, new Long(classPK),
3378:                                entries);
3379:                    }
3380:                } catch (Exception e) {
3381:                    throw new SystemException(e);
3382:                }
3383:            }
3384:
3385:            protected void readTags(PortletDataContext context, Element root)
3386:                    throws PortalException, SystemException {
3387:
3388:                Iterator itr = root.elements("tags").iterator();
3389:
3390:                while (itr.hasNext()) {
3391:                    Element el = (Element) itr.next();
3392:
3393:                    String className = GetterUtil.getString(el
3394:                            .attributeValue("class-name"));
3395:                    long classPK = GetterUtil.getLong(el
3396:                            .attributeValue("class-pk"));
3397:                    String entries = GetterUtil.getString(el
3398:                            .attributeValue("entries"));
3399:
3400:                    context.addTagsEntries(className, new Long(classPK),
3401:                            StringUtil.split(entries, ","));
3402:                }
3403:            }
3404:
3405:            protected void setLocalizedAttributes(Layout layout,
3406:                    Map localeNamesMap, Map localeTitlesMap) {
3407:
3408:                Locale[] locales = LanguageUtil.getAvailableLocales();
3409:
3410:                for (int i = 0; i < locales.length; i++) {
3411:                    String name = (String) localeNamesMap.get(locales[i]);
3412:                    String title = (String) localeTitlesMap.get(locales[i]);
3413:
3414:                    layout.setName(name, locales[i]);
3415:                    layout.setTitle(title, locales[i]);
3416:                }
3417:            }
3418:
3419:            protected void validate(long groupId, boolean privateLayout,
3420:                    long layoutId, long parentLayoutId, String type,
3421:                    boolean hidden, String friendlyURL) throws PortalException,
3422:                    SystemException {
3423:
3424:                boolean firstLayout = false;
3425:
3426:                if (parentLayoutId == LayoutImpl.DEFAULT_PARENT_LAYOUT_ID) {
3427:                    List layouts = layoutPersistence.findByG_P_P(groupId,
3428:                            privateLayout, parentLayoutId, 0, 1);
3429:
3430:                    if (layouts.size() == 0) {
3431:                        firstLayout = true;
3432:                    } else {
3433:                        long firstLayoutId = ((Layout) layouts.get(0))
3434:                                .getLayoutId();
3435:
3436:                        if (firstLayoutId == layoutId) {
3437:                            firstLayout = true;
3438:                        }
3439:                    }
3440:                }
3441:
3442:                if (firstLayout) {
3443:                    validateFirstLayout(type, hidden);
3444:                }
3445:
3446:                if (!PortalUtil.isLayoutParentable(type)) {
3447:                    if (layoutPersistence.countByG_P_P(groupId, privateLayout,
3448:                            layoutId) > 0) {
3449:
3450:                        throw new LayoutTypeException(
3451:                                LayoutTypeException.NOT_PARENTABLE);
3452:                    }
3453:                }
3454:
3455:                if (Validator.isNotNull(friendlyURL)) {
3456:                    int exceptionType = LayoutImpl
3457:                            .validateFriendlyURL(friendlyURL);
3458:
3459:                    if (exceptionType != -1) {
3460:                        throw new LayoutFriendlyURLException(exceptionType);
3461:                    }
3462:
3463:                    try {
3464:                        Layout layout = layoutPersistence.findByG_P_F(groupId,
3465:                                privateLayout, friendlyURL);
3466:
3467:                        if (layout.getLayoutId() != layoutId) {
3468:                            throw new LayoutFriendlyURLException(
3469:                                    LayoutFriendlyURLException.DUPLICATE);
3470:                        }
3471:                    } catch (NoSuchLayoutException nsle) {
3472:                    }
3473:
3474:                    LayoutImpl.validateFriendlyURLKeyword(friendlyURL);
3475:
3476:                    List friendlyURLMappers = portletLocalService
3477:                            .getFriendlyURLMappers();
3478:
3479:                    Iterator itr = friendlyURLMappers.iterator();
3480:
3481:                    while (itr.hasNext()) {
3482:                        FriendlyURLMapper friendlyURLMapper = (FriendlyURLMapper) itr
3483:                                .next();
3484:
3485:                        if (friendlyURL.indexOf(friendlyURLMapper.getMapping()) != -1) {
3486:                            LayoutFriendlyURLException lfurle = new LayoutFriendlyURLException(
3487:                                    LayoutFriendlyURLException.KEYWORD_CONFLICT);
3488:
3489:                            lfurle.setKeywordConflict(friendlyURLMapper
3490:                                    .getMapping());
3491:
3492:                            throw lfurle;
3493:                        }
3494:                    }
3495:
3496:                }
3497:            }
3498:
3499:            protected void validateFirstLayout(String type, boolean hidden)
3500:                    throws PortalException {
3501:
3502:                if (!type.equals(LayoutImpl.TYPE_PORTLET)) {
3503:                    throw new LayoutTypeException(
3504:                            LayoutTypeException.FIRST_LAYOUT);
3505:                }
3506:
3507:                if (hidden) {
3508:                    throw new LayoutHiddenException();
3509:                }
3510:            }
3511:
3512:            protected void validateParentLayoutId(long groupId,
3513:                    boolean privateLayout, long layoutId, long parentLayoutId)
3514:                    throws PortalException, SystemException {
3515:
3516:                Layout layout = layoutPersistence.findByG_P_L(groupId,
3517:                        privateLayout, layoutId);
3518:
3519:                if (parentLayoutId != layout.getParentLayoutId()) {
3520:
3521:                    // Layouts can always be moved to the root level
3522:
3523:                    if (parentLayoutId == LayoutImpl.DEFAULT_PARENT_LAYOUT_ID) {
3524:                        return;
3525:                    }
3526:
3527:                    // Layout cannot become a child of a layout that is not parentable
3528:
3529:                    Layout parentLayout = layoutPersistence.findByG_P_L(
3530:                            groupId, privateLayout, parentLayoutId);
3531:
3532:                    if (!PortalUtil.isLayoutParentable(parentLayout)) {
3533:                        throw new LayoutParentLayoutIdException(
3534:                                LayoutParentLayoutIdException.NOT_PARENTABLE);
3535:                    }
3536:
3537:                    // Layout cannot become descendant of itself
3538:
3539:                    if (isDescendant(layout, parentLayoutId)) {
3540:                        throw new LayoutParentLayoutIdException(
3541:                                LayoutParentLayoutIdException.SELF_DESCENDANT);
3542:                    }
3543:
3544:                    // If layout is moved, the new first layout must be valid
3545:
3546:                    if (layout.getParentLayoutId() == LayoutImpl.DEFAULT_PARENT_LAYOUT_ID) {
3547:
3548:                        List layouts = layoutPersistence.findByG_P_P(groupId,
3549:                                privateLayout,
3550:                                LayoutImpl.DEFAULT_PARENT_LAYOUT_ID, 0, 2);
3551:
3552:                        // You can only reach this point if there are more than two
3553:                        // layouts at the root level because of the descendant check
3554:
3555:                        long firstLayoutId = ((Layout) layouts.get(0))
3556:                                .getLayoutId();
3557:
3558:                        if (firstLayoutId == layoutId) {
3559:                            Layout secondLayout = (Layout) layouts.get(1);
3560:
3561:                            try {
3562:                                validateFirstLayout(secondLayout.getType(),
3563:                                        secondLayout.getHidden());
3564:                            } catch (LayoutHiddenException lhe) {
3565:                                throw new LayoutParentLayoutIdException(
3566:                                        LayoutParentLayoutIdException.FIRST_LAYOUT_HIDDEN);
3567:                            } catch (LayoutTypeException lte) {
3568:                                throw new LayoutParentLayoutIdException(
3569:                                        LayoutParentLayoutIdException.FIRST_LAYOUT_TYPE);
3570:                            }
3571:                        }
3572:                    }
3573:                }
3574:            }
3575:
3576:            private static Log _log = LogFactory
3577:                    .getLog(LayoutLocalServiceImpl.class);
3578:
3579:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.