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.kernel.json.JSONException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
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.portlet.documentlibrary.NoSuchFileEntryException;
029    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
030    
031    import java.io.Serializable;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    import java.util.Locale;
036    
037    /**
038     * @author Bruno Basto
039     */
040    public class DocumentLibraryFieldRenderer extends BaseFieldRenderer {
041    
042            @Override
043            protected String doRender(Field field, Locale locale) throws Exception {
044                    List<String> values = new ArrayList<String>();
045    
046                    for (Serializable value : field.getValues(locale)) {
047                            String valueString = String.valueOf(value);
048    
049                            if (Validator.isNull(valueString)) {
050                                    continue;
051                            }
052    
053                            values.add(handleJSON(valueString, locale));
054                    }
055    
056                    return StringUtil.merge(values, StringPool.COMMA_AND_SPACE);
057            }
058    
059            @Override
060            protected String doRender(Field field, Locale locale, int valueIndex) {
061                    Serializable value = field.getValue(locale, valueIndex);
062    
063                    if (Validator.isNull(value)) {
064                            return StringPool.BLANK;
065                    }
066    
067                    return handleJSON(String.valueOf(value), locale);
068            }
069    
070            protected String handleJSON(String json, Locale locale) {
071                    JSONObject jsonObject = null;
072    
073                    try {
074                            jsonObject = JSONFactoryUtil.createJSONObject(json);
075                    }
076                    catch (JSONException jsone) {
077                            if (_log.isDebugEnabled()) {
078                                    _log.debug("Unable to parse JSON", jsone);
079                            }
080    
081                            return StringPool.BLANK;
082                    }
083    
084                    long fileEntryGroupId = jsonObject.getLong("groupId");
085                    String fileEntryUUID = jsonObject.getString("uuid");
086    
087                    try {
088                            FileEntry fileEntry = DLAppServiceUtil.getFileEntryByUuidAndGroupId(
089                                    fileEntryUUID, fileEntryGroupId);
090    
091                            return fileEntry.getTitle();
092                    }
093                    catch (Exception e) {
094                            if (e instanceof NoSuchFileEntryException ||
095                                    e instanceof PrincipalException) {
096    
097                                    return LanguageUtil.format(
098                                            locale, "is-temporarily-unavailable", "content");
099                            }
100                    }
101    
102                    return StringPool.BLANK;
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(
106                    DocumentLibraryFieldRenderer.class);
107    
108    }