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.model.impl;
0022:
0023: import com.germinus.easyconf.Filter;
0024:
0025: import com.liferay.portal.SystemException;
0026: import com.liferay.portal.kernel.portlet.PortletLayoutListener;
0027: import com.liferay.portal.kernel.util.ArrayUtil;
0028: import com.liferay.portal.kernel.util.GetterUtil;
0029: import com.liferay.portal.kernel.util.StringPool;
0030: import com.liferay.portal.kernel.util.StringUtil;
0031: import com.liferay.portal.kernel.util.Validator;
0032: import com.liferay.portal.model.Group;
0033: import com.liferay.portal.model.Layout;
0034: import com.liferay.portal.model.LayoutTemplate;
0035: import com.liferay.portal.model.LayoutTypePortlet;
0036: import com.liferay.portal.model.Portlet;
0037: import com.liferay.portal.model.PortletPreferences;
0038: import com.liferay.portal.service.PluginSettingLocalServiceUtil;
0039: import com.liferay.portal.service.PortletLocalServiceUtil;
0040: import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
0041: import com.liferay.portal.service.impl.LayoutTemplateLocalUtil;
0042: import com.liferay.portal.util.PortletKeys;
0043: import com.liferay.portal.util.PropsUtil;
0044: import com.liferay.util.ListUtil;
0045: import com.liferay.util.PwdGenerator;
0046:
0047: import java.util.ArrayList;
0048: import java.util.Iterator;
0049: import java.util.List;
0050: import java.util.Properties;
0051:
0052: import org.apache.commons.logging.Log;
0053: import org.apache.commons.logging.LogFactory;
0054:
0055: /**
0056: * <a href="LayoutTypePortletImpl.java.html"><b><i>View Source</i></b></a>
0057: *
0058: * @author Brian Wing Shun Chan
0059: * @author Berentey Zsolt
0060: * @author Jorge Ferrer
0061: *
0062: */
0063: public class LayoutTypePortletImpl extends LayoutTypeImpl implements
0064: LayoutTypePortlet {
0065:
0066: public static final String LAYOUT_TEMPLATE_ID = "layout-template-id";
0067:
0068: public static final String NESTED_COLUMN_IDS = "nested-column-ids";
0069:
0070: public static final String STATE_MAX = "state-max";
0071:
0072: public static final String STATE_MAX_PREVIOUS = "state-max-previous";
0073:
0074: public static final String STATE_MIN = "state-min";
0075:
0076: public static final String MODE_ABOUT = "mode-about";
0077:
0078: public static final String MODE_CONFIG = "mode-config";
0079:
0080: public static final String MODE_EDIT = "mode-edit";
0081:
0082: public static final String MODE_EDIT_DEFAULTS = "mode-edit-defaults";
0083:
0084: public static final String MODE_EDIT_GUEST = "mode-edit-guest";
0085:
0086: public static final String MODE_HELP = "mode-help";
0087:
0088: public static final String MODE_PREVIEW = "mode-preview";
0089:
0090: public static final String MODE_PRINT = "mode-print";
0091:
0092: public static String getFullInstanceSeparator() {
0093: String instanceId = PwdGenerator.getPassword(PwdGenerator.KEY1
0094: + PwdGenerator.KEY2 + PwdGenerator.KEY3, 4);
0095:
0096: return PortletImpl.INSTANCE_SEPARATOR + instanceId;
0097: }
0098:
0099: public LayoutTypePortletImpl(LayoutImpl layout) {
0100: super (layout);
0101: }
0102:
0103: public LayoutTemplate getLayoutTemplate() {
0104: LayoutTemplate layoutTemplate = LayoutTemplateLocalUtil
0105: .getLayoutTemplate(getLayoutTemplateId(), false, null);
0106:
0107: if (layoutTemplate == null) {
0108: layoutTemplate = new LayoutTemplateImpl(StringPool.BLANK,
0109: StringPool.BLANK);
0110:
0111: List columns = new ArrayList();
0112:
0113: for (int i = 1; i <= 10; i++) {
0114: columns.add("column-" + i);
0115: }
0116:
0117: layoutTemplate.setColumns(columns);
0118: }
0119:
0120: return layoutTemplate;
0121: }
0122:
0123: public String getLayoutTemplateId() {
0124: String layoutTemplateId = getTypeSettingsProperties()
0125: .getProperty(LAYOUT_TEMPLATE_ID);
0126:
0127: if (Validator.isNull(layoutTemplateId)) {
0128: layoutTemplateId = "2_columns_ii";
0129:
0130: getTypeSettingsProperties().setProperty(LAYOUT_TEMPLATE_ID,
0131: layoutTemplateId);
0132:
0133: _log.warn("Layout template id for layout "
0134: + getLayout().getPrimaryKey()
0135: + " is null, setting it to 2_columns_ii");
0136: }
0137:
0138: return layoutTemplateId;
0139: }
0140:
0141: public void setLayoutTemplateId(long userId,
0142: String newLayoutTemplateId) {
0143: setLayoutTemplateId(userId, newLayoutTemplateId, true);
0144: }
0145:
0146: public void setLayoutTemplateId(long userId,
0147: String newLayoutTemplateId, boolean checkPermission) {
0148:
0149: if (checkPermission
0150: && !PluginSettingLocalServiceUtil.hasPermission(userId,
0151: newLayoutTemplateId,
0152: LayoutTemplateImpl.PLUGIN_TYPE)) {
0153:
0154: return;
0155: }
0156:
0157: String oldLayoutTemplateId = getLayoutTemplateId();
0158:
0159: getTypeSettingsProperties().setProperty(LAYOUT_TEMPLATE_ID,
0160: newLayoutTemplateId);
0161:
0162: if (Validator.isNull(oldLayoutTemplateId)) {
0163: return;
0164: }
0165:
0166: String themeId = null;
0167:
0168: try {
0169: themeId = getLayout().getTheme().getThemeId();
0170: } catch (Exception e) {
0171: _log.error(e);
0172: }
0173:
0174: LayoutTemplate oldLayoutTemplate = LayoutTemplateLocalUtil
0175: .getLayoutTemplate(oldLayoutTemplateId, false, themeId);
0176:
0177: if (oldLayoutTemplate == null) {
0178: return;
0179: }
0180:
0181: LayoutTemplate newLayoutTemplate = LayoutTemplateLocalUtil
0182: .getLayoutTemplate(newLayoutTemplateId, false, themeId);
0183:
0184: List oldColumns = oldLayoutTemplate.getColumns();
0185: List newColumns = newLayoutTemplate.getColumns();
0186:
0187: reorganizePortlets(newColumns, oldColumns);
0188: }
0189:
0190: public int getNumOfColumns() {
0191: return getLayoutTemplate().getColumns().size();
0192: }
0193:
0194: public List getAllPortlets(String columnId) throws SystemException {
0195: String columnValue = getTypeSettingsProperties().getProperty(
0196: columnId);
0197:
0198: String[] portletIds = StringUtil.split(columnValue);
0199:
0200: List portlets = new ArrayList(portletIds.length);
0201:
0202: for (int i = 0; i < portletIds.length; i++) {
0203: Portlet portlet = PortletLocalServiceUtil.getPortletById(
0204: getLayout().getCompanyId(), portletIds[i]);
0205:
0206: if (portlet != null) {
0207: portlets.add(portlet);
0208: }
0209: }
0210:
0211: List startPortlets = getStaticPortlets(PropsUtil.LAYOUT_STATIC_PORTLETS_START
0212: + columnId);
0213:
0214: List endPortlets = getStaticPortlets(PropsUtil.LAYOUT_STATIC_PORTLETS_END
0215: + columnId);
0216:
0217: return addStaticPortlets(portlets, startPortlets, endPortlets);
0218: }
0219:
0220: public List addStaticPortlets(List portlets, List startPortlets,
0221: List endPortlets) throws SystemException {
0222:
0223: // Return the original array of portlets if no static portlets are
0224: // specified
0225:
0226: if (startPortlets == null) {
0227: startPortlets = new ArrayList();
0228: }
0229:
0230: if (endPortlets == null) {
0231: endPortlets = new ArrayList();
0232: }
0233:
0234: if ((startPortlets.isEmpty()) && (endPortlets.isEmpty())) {
0235: return portlets;
0236: }
0237:
0238: // New array of portlets that contain the static portlets
0239:
0240: List list = new ArrayList(portlets.size()
0241: + startPortlets.size() + endPortlets.size());
0242:
0243: if (startPortlets != null) {
0244: list.addAll(startPortlets);
0245: }
0246:
0247: for (int i = 0; i < portlets.size(); i++) {
0248: Portlet portlet = (Portlet) portlets.get(i);
0249:
0250: // Add the portlet if and only if it is not also a static portlet
0251:
0252: if (!startPortlets.contains(portlet)
0253: && !endPortlets.contains(portlet)) {
0254:
0255: list.add(portlet);
0256: }
0257: }
0258:
0259: if (endPortlets != null) {
0260: list.addAll(endPortlets);
0261: }
0262:
0263: return list;
0264: }
0265:
0266: // Modify portlets
0267:
0268: public String addPortletId(long userId, String portletId) {
0269: return addPortletId(userId, portletId, true);
0270: }
0271:
0272: public String addPortletId(long userId, String portletId,
0273: boolean checkPermission) {
0274:
0275: return addPortletId(userId, portletId, null, -1,
0276: checkPermission);
0277: }
0278:
0279: public String addPortletId(long userId, String portletId,
0280: String columnId, int columnPos) {
0281:
0282: return addPortletId(userId, portletId, columnId, columnPos,
0283: true);
0284: }
0285:
0286: public String addPortletId(long userId, String portletId,
0287: String columnId, int columnPos, boolean checkPermission) {
0288:
0289: Layout layout = getLayout();
0290:
0291: Portlet portlet = null;
0292:
0293: try {
0294: portlet = PortletLocalServiceUtil.getPortletById(layout
0295: .getCompanyId(), portletId);
0296:
0297: if (portlet == null) {
0298: _log
0299: .error("Portlet "
0300: + portletId
0301: + " cannot be added because it is not registered");
0302: }
0303:
0304: if (checkPermission
0305: && !portlet.hasAddPortletPermission(userId)) {
0306: return null;
0307: }
0308: } catch (Exception e) {
0309: _log.error(e);
0310: }
0311:
0312: if (portlet != null) {
0313: if (portlet.isSystem()) {
0314: return null;
0315: }
0316:
0317: if ((portlet.isInstanceable())
0318: && (PortletImpl.getInstanceId(portlet
0319: .getPortletId()) == null)) {
0320:
0321: portletId = portletId + getFullInstanceSeparator();
0322: }
0323:
0324: if (hasPortletId(portletId)) {
0325: return null;
0326: }
0327:
0328: if (columnId == null) {
0329: LayoutTemplate layoutTemplate = getLayoutTemplate();
0330:
0331: List columns = layoutTemplate.getColumns();
0332:
0333: if (columns.size() > 0) {
0334: columnId = (String) columns.get(0);
0335: }
0336: }
0337:
0338: if (columnId != null) {
0339: String columnValue = getTypeSettingsProperties()
0340: .getProperty(columnId);
0341:
0342: if ((columnValue == null)
0343: && (columnId
0344: .startsWith(PortletKeys.NESTED_PORTLETS))) {
0345:
0346: addNestedColumn(columnId);
0347: }
0348:
0349: if (columnPos >= 0) {
0350: List portletIds = ListUtil.fromArray(StringUtil
0351: .split(columnValue));
0352:
0353: if (columnPos <= portletIds.size()) {
0354: portletIds.add(columnPos, portletId);
0355: } else {
0356: portletIds.add(portletId);
0357: }
0358:
0359: columnValue = StringUtil.merge(portletIds);
0360: } else {
0361: columnValue = StringUtil
0362: .add(columnValue, portletId);
0363: }
0364:
0365: getTypeSettingsProperties().setProperty(columnId,
0366: columnValue);
0367: }
0368:
0369: try {
0370: PortletLayoutListener portletLayoutListener = (PortletLayoutListener) portlet
0371: .getPortletLayoutListener();
0372:
0373: if (_enablePortletLayoutListener
0374: && (portletLayoutListener != null)) {
0375:
0376: portletLayoutListener.onAddToLayout(portletId,
0377: layout.getPlid());
0378: }
0379: } catch (Exception e) {
0380: _log.error(
0381: "Unable to fire portlet layout listener event",
0382: e);
0383: }
0384:
0385: return portletId;
0386: } else {
0387: return null;
0388: }
0389: }
0390:
0391: public void addPortletIds(long userId, String[] portletIds,
0392: boolean checkPermission) {
0393:
0394: for (int i = 0; i < portletIds.length; i++) {
0395: String portletId = portletIds[i];
0396:
0397: addPortletId(userId, portletId, checkPermission);
0398: }
0399: }
0400:
0401: public void addPortletIds(long userId, String[] portletIds,
0402: String columnId, boolean checkPermission) {
0403:
0404: for (int i = 0; i < portletIds.length; i++) {
0405: String portletId = portletIds[i];
0406:
0407: addPortletId(userId, portletId, columnId, -1,
0408: checkPermission);
0409: }
0410: }
0411:
0412: public List getPortlets() throws SystemException {
0413: List portletIds = getPortletIds();
0414:
0415: List portlets = new ArrayList(portletIds.size());
0416:
0417: for (int i = 0; i < portletIds.size(); i++) {
0418: String portletId = (String) portletIds.get(i);
0419:
0420: Portlet portlet = PortletLocalServiceUtil.getPortletById(
0421: getLayout().getCompanyId(), portletId);
0422:
0423: if (portlet != null) {
0424: portlets.add(portlet);
0425: }
0426: }
0427:
0428: return portlets;
0429: }
0430:
0431: public List getPortletIds() {
0432: List portletIds = new ArrayList();
0433:
0434: List columns = getColumns();
0435:
0436: for (int i = 0; i < columns.size(); i++) {
0437: String columnId = (String) columns.get(i);
0438:
0439: String columnValue = getTypeSettingsProperties()
0440: .getProperty(columnId);
0441:
0442: portletIds.addAll(ListUtil.fromArray(StringUtil
0443: .split(columnValue)));
0444:
0445: }
0446:
0447: return portletIds;
0448: }
0449:
0450: public boolean hasPortletId(String portletId) {
0451: List columns = getColumns();
0452:
0453: for (int i = 0; i < columns.size(); i++) {
0454: String columnId = (String) columns.get(i);
0455:
0456: if (hasNonstaticPortletId(columnId, portletId)) {
0457: return true;
0458: }
0459:
0460: if (hasStaticPortletId(columnId, portletId)) {
0461: return true;
0462: }
0463: }
0464:
0465: return false;
0466: }
0467:
0468: public void movePortletId(long userId, String portletId,
0469: String columnId, int columnPos) {
0470:
0471: _enablePortletLayoutListener = false;
0472:
0473: try {
0474: removePortletId(portletId, false);
0475: addPortletId(userId, portletId, columnId, columnPos);
0476: } finally {
0477: _enablePortletLayoutListener = true;
0478: }
0479:
0480: Layout layout = getLayout();
0481:
0482: try {
0483: Portlet portlet = PortletLocalServiceUtil.getPortletById(
0484: layout.getCompanyId(), portletId);
0485:
0486: if (portlet != null) {
0487: PortletLayoutListener portletLayoutListener = (PortletLayoutListener) portlet
0488: .getPortletLayoutListener();
0489:
0490: if (portletLayoutListener != null) {
0491: portletLayoutListener.onMoveInLayout(portletId,
0492: layout.getPlid());
0493: }
0494: }
0495: } catch (Exception e) {
0496: _log.error("Unable to fire portlet layout listener event",
0497: e);
0498: }
0499: }
0500:
0501: public void removePortletId(String portletId) {
0502: removePortletId(portletId, true);
0503: }
0504:
0505: public void removePortletId(String portletId, boolean cleanUp) {
0506: List columns = getColumns();
0507:
0508: for (int i = 0; i < columns.size(); i++) {
0509: String columnId = (String) columns.get(i);
0510:
0511: String columnValue = getTypeSettingsProperties()
0512: .getProperty(columnId);
0513:
0514: columnValue = StringUtil.remove(columnValue, portletId);
0515:
0516: getTypeSettingsProperties().setProperty(columnId,
0517: columnValue);
0518: }
0519:
0520: if (cleanUp) {
0521: removeStatesPortletId(portletId);
0522: removeModesPortletId(portletId);
0523:
0524: try {
0525: onRemoveFromLayout(portletId);
0526: } catch (Exception e) {
0527: _log.error(
0528: "Unable to fire portlet layout listener event",
0529: e);
0530: }
0531: }
0532: }
0533:
0534: public void setPortletIds(String columnId, String portletIds) {
0535: getTypeSettingsProperties().setProperty(columnId, portletIds);
0536: }
0537:
0538: public void reorganizeNestedColumns(String portletId,
0539: List newColumns, List oldColumns) {
0540:
0541: String nestedColumnIds = getTypeSettingsProperties()
0542: .getProperty(NESTED_COLUMN_IDS);
0543:
0544: String[] nestedColumnIdsArray = StringUtil
0545: .split(nestedColumnIds);
0546:
0547: nestedColumnIdsArray = ArrayUtil.removeByPrefix(
0548: nestedColumnIdsArray, portletId);
0549:
0550: getTypeSettingsProperties().setProperty(NESTED_COLUMN_IDS,
0551: StringUtil.merge(nestedColumnIdsArray));
0552:
0553: reorganizePortlets(newColumns, oldColumns);
0554: }
0555:
0556: public void reorganizePortlets(List newColumns, List oldColumns) {
0557: String lastNewColumnId = (String) newColumns.get(newColumns
0558: .size() - 1);
0559: String lastNewColumnValue = getTypeSettingsProperties()
0560: .getProperty(lastNewColumnId);
0561:
0562: Iterator itr = oldColumns.iterator();
0563:
0564: while (itr.hasNext()) {
0565: String oldColumnId = (String) itr.next();
0566:
0567: if (!newColumns.contains(oldColumnId)) {
0568: String oldColumnValue = (String) getTypeSettingsProperties()
0569: .remove(oldColumnId);
0570:
0571: String[] portletIds = StringUtil.split(oldColumnValue);
0572:
0573: for (int i = 0; i < portletIds.length; i++) {
0574: lastNewColumnValue = StringUtil.add(
0575: lastNewColumnValue, portletIds[i]);
0576: }
0577: }
0578: }
0579:
0580: getTypeSettingsProperties().setProperty(lastNewColumnId,
0581: lastNewColumnValue);
0582: }
0583:
0584: // Maximized state
0585:
0586: public String getStateMax() {
0587: return getTypeSettingsProperties().getProperty(STATE_MAX);
0588: }
0589:
0590: public void setStateMax(String stateMax) {
0591: getTypeSettingsProperties().setProperty(STATE_MAX, stateMax);
0592: }
0593:
0594: public boolean hasStateMax() {
0595: String[] stateMax = StringUtil.split(getStateMax());
0596:
0597: if (stateMax.length > 0) {
0598: return true;
0599: } else {
0600: return false;
0601: }
0602: }
0603:
0604: public void addStateMaxPortletId(String portletId) {
0605: removeStatesPortletId(portletId);
0606: //setStateMax(StringUtil.add(getStateMax(), portletId));
0607: setStateMax(StringUtil.add(StringPool.BLANK, portletId));
0608: }
0609:
0610: public String getStateMaxPortletId() {
0611: String[] stateMax = StringUtil.split(getStateMax());
0612:
0613: if (stateMax.length > 0) {
0614: return stateMax[0];
0615: } else {
0616: return StringPool.BLANK;
0617: }
0618: }
0619:
0620: public boolean hasStateMaxPortletId(String portletId) {
0621: if (StringUtil.contains(getStateMax(), portletId)) {
0622: return true;
0623: } else {
0624: return false;
0625: }
0626: }
0627:
0628: public void removeStateMaxPortletId(String portletId) {
0629: setStateMax(StringUtil.remove(getStateMax(), portletId));
0630: }
0631:
0632: // Maximized state previous
0633:
0634: public String getStateMaxPrevious() {
0635: return getTypeSettingsProperties().getProperty(
0636: STATE_MAX_PREVIOUS);
0637: }
0638:
0639: public void setStateMaxPrevious(String stateMaxPrevious) {
0640: getTypeSettingsProperties().setProperty(STATE_MAX_PREVIOUS,
0641: stateMaxPrevious);
0642: }
0643:
0644: public void removeStateMaxPrevious() {
0645: getTypeSettingsProperties().remove(STATE_MAX_PREVIOUS);
0646: }
0647:
0648: // Minimized state
0649:
0650: public String getStateMin() {
0651: return getTypeSettingsProperties().getProperty(STATE_MIN);
0652: }
0653:
0654: public void setStateMin(String stateMin) {
0655: getTypeSettingsProperties().setProperty(STATE_MIN, stateMin);
0656: }
0657:
0658: public boolean hasStateMin() {
0659: String[] stateMin = StringUtil.split(getStateMin());
0660:
0661: if (stateMin.length > 0) {
0662: return true;
0663: } else {
0664: return false;
0665: }
0666: }
0667:
0668: public void addStateMinPortletId(String portletId) {
0669: removeStateMaxPortletId(portletId);
0670: setStateMin(StringUtil.add(getStateMin(), portletId));
0671: }
0672:
0673: public boolean hasStateMinPortletId(String portletId) {
0674: if (StringUtil.contains(getStateMin(), portletId)) {
0675: return true;
0676: } else {
0677: return false;
0678: }
0679: }
0680:
0681: public void removeStateMinPortletId(String portletId) {
0682: setStateMin(StringUtil.remove(getStateMin(), portletId));
0683: }
0684:
0685: // Normal state
0686:
0687: public boolean hasStateNormalPortletId(String portletId) {
0688: if (hasStateMaxPortletId(portletId)
0689: || hasStateMinPortletId(portletId)) {
0690:
0691: return false;
0692: } else {
0693: return true;
0694: }
0695: }
0696:
0697: // All states
0698:
0699: public void resetStates() {
0700: setStateMax(StringPool.BLANK);
0701: setStateMin(StringPool.BLANK);
0702: }
0703:
0704: public void removeStatesPortletId(String portletId) {
0705: removeStateMaxPortletId(portletId);
0706: removeStateMinPortletId(portletId);
0707: }
0708:
0709: // About mode
0710:
0711: public String getModeAbout() {
0712: return getTypeSettingsProperties().getProperty(MODE_ABOUT);
0713: }
0714:
0715: public void setModeAbout(String modeAbout) {
0716: getTypeSettingsProperties().setProperty(MODE_ABOUT, modeAbout);
0717: }
0718:
0719: public void addModeAboutPortletId(String portletId) {
0720: removeModesPortletId(portletId);
0721: setModeAbout(StringUtil.add(getModeAbout(), portletId));
0722: }
0723:
0724: public boolean hasModeAboutPortletId(String portletId) {
0725: return StringUtil.contains(getModeAbout(), portletId);
0726: }
0727:
0728: public void removeModeAboutPortletId(String portletId) {
0729: setModeAbout(StringUtil.remove(getModeAbout(), portletId));
0730: }
0731:
0732: // Config mode
0733:
0734: public String getModeConfig() {
0735: return getTypeSettingsProperties().getProperty(MODE_CONFIG);
0736: }
0737:
0738: public void setModeConfig(String modeConfig) {
0739: getTypeSettingsProperties()
0740: .setProperty(MODE_CONFIG, modeConfig);
0741: }
0742:
0743: public void addModeConfigPortletId(String portletId) {
0744: removeModesPortletId(portletId);
0745: setModeConfig(StringUtil.add(getModeConfig(), portletId));
0746: }
0747:
0748: public boolean hasModeConfigPortletId(String portletId) {
0749: return StringUtil.contains(getModeConfig(), portletId);
0750: }
0751:
0752: public void removeModeConfigPortletId(String portletId) {
0753: setModeConfig(StringUtil.remove(getModeConfig(), portletId));
0754: }
0755:
0756: // Edit mode
0757:
0758: public String getModeEdit() {
0759: return getTypeSettingsProperties().getProperty(MODE_EDIT);
0760: }
0761:
0762: public void setModeEdit(String modeEdit) {
0763: getTypeSettingsProperties().setProperty(MODE_EDIT, modeEdit);
0764: }
0765:
0766: public void addModeEditPortletId(String portletId) {
0767: removeModesPortletId(portletId);
0768: setModeEdit(StringUtil.add(getModeEdit(), portletId));
0769: }
0770:
0771: public boolean hasModeEditPortletId(String portletId) {
0772: return StringUtil.contains(getModeEdit(), portletId);
0773: }
0774:
0775: public void removeModeEditPortletId(String portletId) {
0776: setModeEdit(StringUtil.remove(getModeEdit(), portletId));
0777: }
0778:
0779: // Edit defaults mode
0780:
0781: public String getModeEditDefaults() {
0782: return getTypeSettingsProperties().getProperty(
0783: MODE_EDIT_DEFAULTS);
0784: }
0785:
0786: public void setModeEditDefaults(String modeEditDefaults) {
0787: getTypeSettingsProperties().setProperty(MODE_EDIT_DEFAULTS,
0788: modeEditDefaults);
0789: }
0790:
0791: public void addModeEditDefaultsPortletId(String portletId) {
0792: removeModesPortletId(portletId);
0793: setModeEditDefaults(StringUtil.add(getModeEditDefaults(),
0794: portletId));
0795: }
0796:
0797: public boolean hasModeEditDefaultsPortletId(String portletId) {
0798: return StringUtil.contains(getModeEditDefaults(), portletId);
0799: }
0800:
0801: public void removeModeEditDefaultsPortletId(String portletId) {
0802: setModeEditDefaults(StringUtil.remove(getModeEditDefaults(),
0803: portletId));
0804: }
0805:
0806: // Edit guest mode
0807:
0808: public String getModeEditGuest() {
0809: return getTypeSettingsProperties().getProperty(MODE_EDIT_GUEST);
0810: }
0811:
0812: public void setModeEditGuest(String modeEditGuest) {
0813: getTypeSettingsProperties().setProperty(MODE_EDIT_GUEST,
0814: modeEditGuest);
0815: }
0816:
0817: public void addModeEditGuestPortletId(String portletId) {
0818: removeModesPortletId(portletId);
0819: setModeEditGuest(StringUtil.add(getModeEditGuest(), portletId));
0820: }
0821:
0822: public boolean hasModeEditGuestPortletId(String portletId) {
0823: return StringUtil.contains(getModeEditGuest(), portletId);
0824: }
0825:
0826: public void removeModeEditGuestPortletId(String portletId) {
0827: setModeEditGuest(StringUtil.remove(getModeEditGuest(),
0828: portletId));
0829: }
0830:
0831: // Help mode
0832:
0833: public String getModeHelp() {
0834: return getTypeSettingsProperties().getProperty(MODE_HELP);
0835: }
0836:
0837: public void setModeHelp(String modeHelp) {
0838: getTypeSettingsProperties().setProperty(MODE_HELP, modeHelp);
0839: }
0840:
0841: public void addModeHelpPortletId(String portletId) {
0842: removeModesPortletId(portletId);
0843: setModeHelp(StringUtil.add(getModeHelp(), portletId));
0844: }
0845:
0846: public boolean hasModeHelpPortletId(String portletId) {
0847: return StringUtil.contains(getModeHelp(), portletId);
0848: }
0849:
0850: public void removeModeHelpPortletId(String portletId) {
0851: setModeHelp(StringUtil.remove(getModeHelp(), portletId));
0852: }
0853:
0854: // Preview mode
0855:
0856: public String getModePreview() {
0857: return getTypeSettingsProperties().getProperty(MODE_PREVIEW);
0858: }
0859:
0860: public void setModePreview(String modePreview) {
0861: getTypeSettingsProperties().setProperty(MODE_PREVIEW,
0862: modePreview);
0863: }
0864:
0865: public void addModePreviewPortletId(String portletId) {
0866: removeModesPortletId(portletId);
0867: setModePreview(StringUtil.add(getModePreview(), portletId));
0868: }
0869:
0870: public boolean hasModePreviewPortletId(String portletId) {
0871: return StringUtil.contains(getModePreview(), portletId);
0872: }
0873:
0874: public void removeModePreviewPortletId(String portletId) {
0875: setModePreview(StringUtil.remove(getModePreview(), portletId));
0876: }
0877:
0878: // Print mode
0879:
0880: public String getModePrint() {
0881: return getTypeSettingsProperties().getProperty(MODE_PRINT);
0882: }
0883:
0884: public void setModePrint(String modePrint) {
0885: getTypeSettingsProperties().setProperty(MODE_PRINT, modePrint);
0886: }
0887:
0888: public void addModePrintPortletId(String portletId) {
0889: removeModesPortletId(portletId);
0890: setModePrint(StringUtil.add(getModePrint(), portletId));
0891: }
0892:
0893: public boolean hasModePrintPortletId(String portletId) {
0894: return StringUtil.contains(getModePrint(), portletId);
0895: }
0896:
0897: public void removeModePrintPortletId(String portletId) {
0898: setModePrint(StringUtil.remove(getModePrint(), portletId));
0899: }
0900:
0901: // View mode
0902:
0903: public boolean hasModeViewPortletId(String portletId) {
0904: if (hasModeAboutPortletId(portletId)
0905: || hasModeConfigPortletId(portletId)
0906: || hasModeEditPortletId(portletId)
0907: || hasModeEditDefaultsPortletId(portletId)
0908: || hasModeEditGuestPortletId(portletId)
0909: || hasModeHelpPortletId(portletId)
0910: || hasModePreviewPortletId(portletId)
0911: || hasModePrintPortletId(portletId)) {
0912:
0913: return false;
0914: } else {
0915: return true;
0916: }
0917: }
0918:
0919: // All modes
0920:
0921: public void resetModes() {
0922: setModeAbout(StringPool.BLANK);
0923: setModeConfig(StringPool.BLANK);
0924: setModeEdit(StringPool.BLANK);
0925: setModeEditDefaults(StringPool.BLANK);
0926: setModeEditGuest(StringPool.BLANK);
0927: setModeHelp(StringPool.BLANK);
0928: setModePreview(StringPool.BLANK);
0929: setModePrint(StringPool.BLANK);
0930: }
0931:
0932: public void removeModesPortletId(String portletId) {
0933: removeModeAboutPortletId(portletId);
0934: removeModeConfigPortletId(portletId);
0935: removeModeEditPortletId(portletId);
0936: removeModeEditDefaultsPortletId(portletId);
0937: removeModeEditGuestPortletId(portletId);
0938: removeModeHelpPortletId(portletId);
0939: removeModePreviewPortletId(portletId);
0940: removeModePrintPortletId(portletId);
0941: }
0942:
0943: public void removeNestedColumns(String portletId) {
0944: Properties props = getTypeSettingsProperties();
0945:
0946: Iterator itr = props.keySet().iterator();
0947:
0948: Properties newProps = new Properties();
0949:
0950: while (itr.hasNext()) {
0951: String key = (String) itr.next();
0952:
0953: if (!key.startsWith(portletId)) {
0954: newProps.setProperty(key, props.getProperty(key));
0955: }
0956: }
0957:
0958: getLayout().setTypeSettingsProperties(newProps);
0959:
0960: String nestedColumnIds = GetterUtil
0961: .getString(getTypeSettingsProperties().getProperty(
0962: NESTED_COLUMN_IDS));
0963:
0964: String[] nestedColumnIdsArray = ArrayUtil.removeByPrefix(
0965: StringUtil.split(nestedColumnIds), portletId);
0966:
0967: getTypeSettingsProperties().setProperty(NESTED_COLUMN_IDS,
0968: StringUtil.merge(nestedColumnIdsArray));
0969: }
0970:
0971: protected void addNestedColumn(String columnId) {
0972: String nestedColumnIds = getTypeSettingsProperties()
0973: .getProperty(NESTED_COLUMN_IDS, StringPool.BLANK);
0974:
0975: if (nestedColumnIds.indexOf(columnId) == -1) {
0976: nestedColumnIds = StringUtil.add(nestedColumnIds, columnId);
0977:
0978: getTypeSettingsProperties().setProperty(NESTED_COLUMN_IDS,
0979: nestedColumnIds);
0980: }
0981: }
0982:
0983: protected List getColumns() {
0984: LayoutTemplate layoutTemplate = getLayoutTemplate();
0985:
0986: List columns = new ArrayList();
0987:
0988: columns.addAll(layoutTemplate.getColumns());
0989: columns.addAll(getNestedColumns());
0990:
0991: return columns;
0992: }
0993:
0994: protected List getNestedColumns() {
0995: String nestedColumnIds = getTypeSettingsProperties()
0996: .getProperty(NESTED_COLUMN_IDS);
0997:
0998: return ListUtil.fromArray(StringUtil.split(nestedColumnIds));
0999: }
1000:
1001: protected String[] getStaticPortletIds(String position) {
1002: Layout layout = getLayout();
1003:
1004: String selector1 = StringPool.BLANK;
1005:
1006: Group group = layout.getGroup();
1007:
1008: if (group.isUser()) {
1009: selector1 = "user";
1010: } else if (group.isCommunity()) {
1011: selector1 = "community";
1012: } else if (group.isOrganization()) {
1013: selector1 = "organization";
1014: }
1015:
1016: String selector2 = StringPool.BLANK;
1017:
1018: if (layout.isFirstParent()) {
1019: selector2 = "firstLayout";
1020: }
1021:
1022: return PropsUtil.getComponentProperties().getStringArray(
1023: position, Filter.by(selector1, selector2));
1024: }
1025:
1026: protected List getStaticPortlets(String position)
1027: throws SystemException {
1028: String[] portletIds = getStaticPortletIds(position);
1029:
1030: List portlets = new ArrayList();
1031:
1032: for (int i = 0; i < portletIds.length; i++) {
1033: String portletId = portletIds[i];
1034:
1035: if (hasNonstaticPortletId(portletId)) {
1036: continue;
1037: }
1038:
1039: Portlet portlet = PortletLocalServiceUtil.getPortletById(
1040: getLayout().getCompanyId(), portletId);
1041:
1042: if (portlet != null) {
1043: Portlet staticPortlet = portlet;
1044:
1045: if (portlet.isInstanceable()) {
1046:
1047: // Instanceable portlets do not need to be cloned because
1048: // they are already cloned. See the method getPortletById in
1049: // the class PortletLocalServiceImpl and how it references
1050: // the method getClonedInstance in the class PortletImpl.
1051:
1052: } else {
1053: staticPortlet = (Portlet) staticPortlet.clone();
1054: }
1055:
1056: staticPortlet.setStatic(true);
1057:
1058: if (position.startsWith("layout.static.portlets.start")) {
1059: staticPortlet.setStaticStart(true);
1060: }
1061:
1062: portlets.add(staticPortlet);
1063: }
1064: }
1065:
1066: return portlets;
1067: }
1068:
1069: protected boolean hasNonstaticPortletId(String portletId) {
1070: LayoutTemplate layoutTemplate = getLayoutTemplate();
1071:
1072: List columns = layoutTemplate.getColumns();
1073:
1074: for (int i = 0; i < columns.size(); i++) {
1075: String columnId = (String) columns.get(i);
1076:
1077: if (hasNonstaticPortletId(columnId, portletId)) {
1078: return true;
1079: }
1080: }
1081:
1082: return false;
1083: }
1084:
1085: protected boolean hasNonstaticPortletId(String columnId,
1086: String portletId) {
1087: String columnValue = getTypeSettingsProperties().getProperty(
1088: columnId);
1089:
1090: if (StringUtil.contains(columnValue, portletId)) {
1091: return true;
1092: } else {
1093: return false;
1094: }
1095: }
1096:
1097: /*protected boolean hasStaticPortletId(String portletId) {
1098: LayoutTemplate layoutTemplate = getLayoutTemplate();
1099:
1100: List columns = layoutTemplate.getColumns();
1101:
1102: for (int i = 0; i < columns.size(); i++) {
1103: String columnId = (String)columns.get(i);
1104:
1105: if (hasStaticPortletId(columnId, portletId)) {
1106: return true;
1107: }
1108: }
1109:
1110: return false;
1111: }*/
1112:
1113: protected boolean hasStaticPortletId(String columnId,
1114: String portletId) {
1115: String[] staticPortletIdsStart = getStaticPortletIds(PropsUtil.LAYOUT_STATIC_PORTLETS_START
1116: + columnId);
1117:
1118: String[] staticPortletIdsEnd = getStaticPortletIds(PropsUtil.LAYOUT_STATIC_PORTLETS_END
1119: + columnId);
1120:
1121: String[] staticPortletIds = ArrayUtil.append(
1122: staticPortletIdsStart, staticPortletIdsEnd);
1123:
1124: for (int i = 0; i < staticPortletIds.length; i++) {
1125: String staticPortletId = staticPortletIds[i];
1126:
1127: if (staticPortletId.equals(portletId)) {
1128: return true;
1129: }
1130: }
1131:
1132: return false;
1133: }
1134:
1135: protected void onRemoveFromLayout(String portletId)
1136: throws SystemException {
1137: Portlet portlet = PortletLocalServiceUtil.getPortletById(
1138: getLayout().getCompanyId(), portletId);
1139:
1140: if (portlet == null) {
1141: return;
1142: }
1143:
1144: if (portlet.getRootPortletId().equals(
1145: PortletKeys.NESTED_PORTLETS)) {
1146: Properties props = getTypeSettingsProperties();
1147:
1148: Iterator itr = props.keySet().iterator();
1149:
1150: while (itr.hasNext()) {
1151: String key = (String) itr.next();
1152:
1153: if (key.startsWith(portlet.getPortletId())) {
1154: String portletIds = props.getProperty(key);
1155:
1156: String[] portletIdsArray = StringUtil
1157: .split(portletIds);
1158:
1159: for (int i = 0; i < portletIdsArray.length; i++) {
1160: onRemoveFromLayout(portletIdsArray[i]);
1161: }
1162: }
1163: }
1164:
1165: removeNestedColumns(portletId);
1166: }
1167:
1168: if (_enablePortletLayoutListener) {
1169: PortletLayoutListener portletLayoutListener = portlet
1170: .getPortletLayoutListener();
1171:
1172: long plid = getLayout().getPlid();
1173:
1174: if ((portletLayoutListener != null)) {
1175: portletLayoutListener.onRemoveFromLayout(portletId,
1176: plid);
1177: }
1178: }
1179:
1180: deletePortletSetup(portletId);
1181: }
1182:
1183: protected void deletePortletSetup(String portletId) {
1184: try {
1185: List list = PortletPreferencesLocalServiceUtil
1186: .getPortletPreferences(getLayout().getPlid(),
1187: portletId);
1188:
1189: for (int i = 0; i < list.size(); i++) {
1190: PortletPreferences portletPreferences = (PortletPreferences) list
1191: .get(i);
1192:
1193: PortletPreferencesLocalServiceUtil
1194: .deletePortletPreferences(portletPreferences
1195: .getPortletPreferencesId());
1196: }
1197: } catch (Exception e) {
1198: _log.error(e, e);
1199: }
1200: }
1201:
1202: private static Log _log = LogFactory
1203: .getLog(LayoutTypePortletImpl.class);
1204:
1205: private boolean _enablePortletLayoutListener = true;
1206:
1207: }
|