001    /**
002     * Copyright (c) 2000-2011 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.util.Iterator;
022    import java.util.Map;
023    import java.util.Set;
024    import java.util.TreeMap;
025    
026    /**
027     * @author Jorge Ferrer
028     */
029    public class RepositoryReport {
030    
031            public static final String SUCCESS = "success";
032    
033            public void addSuccess(String repositoryURL) {
034                    _reportMap.put(repositoryURL, SUCCESS);
035            }
036    
037            public void addError(String repositoryURL, PluginPackageException ppe) {
038                    StringBundler sb = new StringBundler(3);
039    
040                    if (Validator.isNotNull(ppe.getMessage())) {
041                            sb.append(ppe.getMessage());
042                    }
043    
044                    if ((ppe.getCause() != null) &&
045                            Validator.isNull(ppe.getCause().getMessage())) {
046    
047                            sb.append(ppe.getCause().getMessage());
048                    }
049    
050                    if (sb.length() == 0) {
051                            sb.append(ppe.toString());
052                    }
053    
054                    _reportMap.put(repositoryURL, sb.toString());
055            }
056    
057            public Set<String> getRepositoryURLs() {
058                    return _reportMap.keySet();
059            }
060    
061            public String getState(String repositoryURL) {
062                    return _reportMap.get(repositoryURL);
063            }
064    
065            public String toString() {
066                    Iterator<String> itr = getRepositoryURLs().iterator();
067    
068                    if (getRepositoryURLs().isEmpty()) {
069                            return StringPool.BLANK;
070                    }
071    
072                    StringBundler sb = new StringBundler(getRepositoryURLs().size() * 3);
073    
074                    while (itr.hasNext()) {
075                            String repositoryURL = itr.next();
076    
077                            sb.append(repositoryURL);
078                            sb.append(": ");
079                            sb.append(_reportMap.get(repositoryURL));
080                    }
081    
082                    return sb.toString();
083            }
084    
085            private Map<String, String> _reportMap = new TreeMap<String, String>();
086    
087    }