001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.json.JSONException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.LayoutServiceUtil;
029    
030    import java.io.Serializable;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    import java.util.Locale;
035    
036    /**
037     * @author Bruno Basto
038     */
039    public class LinkToPageFieldRenderer extends BaseFieldRenderer {
040    
041            @Override
042            protected String doRender(Field field, Locale locale) throws Exception {
043                    List<String> values = new ArrayList<String>();
044    
045                    for (Serializable value : field.getValues(locale)) {
046                            String valueString = String.valueOf(value);
047    
048                            if (Validator.isNull(valueString)) {
049                                    continue;
050                            }
051    
052                            values.add(handleJSON(valueString, locale));
053                    }
054    
055                    return StringUtil.merge(values, StringPool.COMMA_AND_SPACE);
056            }
057    
058            @Override
059            protected String doRender(Field field, Locale locale, int valueIndex) {
060                    Serializable value = field.getValue(locale, valueIndex);
061    
062                    if (Validator.isNull(value)) {
063                            return StringPool.BLANK;
064                    }
065    
066                    return handleJSON(String.valueOf(value), locale);
067            }
068    
069            protected String handleJSON(String value, Locale locale) {
070                    JSONObject jsonObject = null;
071    
072                    try {
073                            jsonObject = JSONFactoryUtil.createJSONObject(value);
074                    }
075                    catch (JSONException jsone) {
076                            if (_log.isDebugEnabled()) {
077                                    _log.debug("Unable to parse JSON", jsone);
078                            }
079    
080                            return StringPool.BLANK;
081                    }
082    
083                    long groupId = jsonObject.getLong("groupId");
084                    boolean privateLayout = jsonObject.getBoolean("privateLayout");
085                    long layoutId = jsonObject.getLong("layoutId");
086    
087                    try {
088                            return LayoutServiceUtil.getLayoutName(
089                                    groupId, privateLayout, layoutId,
090                                    LanguageUtil.getLanguageId(locale));
091                    }
092                    catch (Exception e) {
093                            if (e instanceof NoSuchLayoutException ||
094                                    e instanceof PrincipalException) {
095    
096                                    return LanguageUtil.format(
097                                            locale, "is-temporarily-unavailable", "content");
098                            }
099                    }
100    
101                    return StringPool.BLANK;
102            }
103    
104            private static Log _log = LogFactoryUtil.getLog(
105                    LinkToPageFieldRenderer.class);
106    
107    }