001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.plugin;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.io.Serializable;
022    
023    import java.util.Map;
024    import java.util.Set;
025    import java.util.TreeMap;
026    
027    /**
028     * @author Jorge Ferrer
029     */
030    public class RepositoryReport implements Serializable {
031    
032            public static final String SUCCESS = "success";
033    
034            public void addError(String repositoryURL, PluginPackageException ppe) {
035                    StringBundler sb = new StringBundler(2);
036    
037                    if (Validator.isNotNull(ppe.getMessage())) {
038                            sb.append(ppe.getMessage());
039                    }
040    
041                    if (ppe.getCause() != null) {
042                            Throwable cause = ppe.getCause();
043    
044                            if (Validator.isNotNull(cause.getMessage())) {
045                                    sb.append(cause.getMessage());
046                            }
047                    }
048    
049                    if (sb.index() == 0) {
050                            sb.append(ppe.toString());
051                    }
052    
053                    _reportMap.put(repositoryURL, sb.toString());
054            }
055    
056            public void addSuccess(String repositoryURL) {
057                    _reportMap.put(repositoryURL, SUCCESS);
058            }
059    
060            public Set<String> getRepositoryURLs() {
061                    return _reportMap.keySet();
062            }
063    
064            public String getState(String repositoryURL) {
065                    return _reportMap.get(repositoryURL);
066            }
067    
068            @Override
069            public String toString() {
070                    Set<String> repositoryURLs = getRepositoryURLs();
071    
072                    if (repositoryURLs.isEmpty()) {
073                            return StringPool.BLANK;
074                    }
075    
076                    StringBundler sb = new StringBundler(repositoryURLs.size() * 3);
077    
078                    for (String repositoryURL : repositoryURLs) {
079                            sb.append(repositoryURL);
080                            sb.append(": ");
081                            sb.append(_reportMap.get(repositoryURL));
082                    }
083    
084                    return sb.toString();
085            }
086    
087            private Map<String, String> _reportMap = new TreeMap<String, String>();
088    
089    }