001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.sitemap.client.alias;
029
030import static org.opencms.ade.sitemap.shared.I_CmsAliasConstants.JSON_LINE;
031import static org.opencms.ade.sitemap.shared.I_CmsAliasConstants.JSON_MESSAGE;
032import static org.opencms.ade.sitemap.shared.I_CmsAliasConstants.JSON_STATUS;
033
034import org.opencms.gwt.shared.alias.CmsAliasImportStatus;
035
036import java.util.ArrayList;
037import java.util.List;
038
039import com.google.gwt.json.client.JSONArray;
040import com.google.gwt.json.client.JSONObject;
041import com.google.gwt.json.client.JSONString;
042import com.google.gwt.json.client.JSONValue;
043
044/**
045 * A client-side bean for representing the results of an alias import.<p>
046 */
047public class CmsClientAliasImportResult {
048
049    /** The file line from which the alias to import has been read. */
050    private String m_line;
051
052    /** The alias import message. */
053    private String m_message;
054
055    /** The alias import status. */
056    private CmsAliasImportStatus m_status;
057
058    /**
059     * Creates a new instance.<p>
060     *
061     * @param line the CSV line containing the alias
062     * @param status the import status
063     * @param message the import message
064     */
065    public CmsClientAliasImportResult(String line, CmsAliasImportStatus status, String message) {
066
067        m_status = status;
068        m_line = line;
069        m_message = message;
070    }
071
072    /**
073     * Parses an alias import result from a JSON object.<p>
074     *
075     * @param json the JSON object containing the alias import result data
076     *
077     * @return the alias import result bean
078     */
079    public static CmsClientAliasImportResult parse(JSONObject json) {
080
081        String line = getString(json, JSON_LINE);
082        String message = getString(json, JSON_MESSAGE);
083        String statusStr = getString(json, JSON_STATUS);
084        CmsAliasImportStatus status = CmsAliasImportStatus.valueOf(statusStr);
085        return new CmsClientAliasImportResult(line, status, message);
086    }
087
088    /**
089     * Extracts alias import results from a JSON array.<p>
090     *
091     * @param array the JSON array
092     *
093     * @return the alias import results from the array
094     */
095    public static List<CmsClientAliasImportResult> parseArray(JSONArray array) {
096
097        List<CmsClientAliasImportResult> result = new ArrayList<CmsClientAliasImportResult>();
098        for (int i = 0; i < array.size(); i++) {
099            JSONValue lineVal = array.get(i);
100            JSONObject lineObj = (JSONObject)lineVal;
101            CmsClientAliasImportResult singleResult = parse(lineObj);
102            result.add(singleResult);
103        }
104        return result;
105    }
106
107    /**
108     * Helper method to get a string value from a JSON object.<p>
109     *
110     * @param json the JSON object
111     * @param key the key whose value should be extracted as a string
112     *
113     * @return the string value for the given key
114     */
115    protected static String getString(JSONObject json, String key) {
116
117        JSONValue value = json.get(key);
118        JSONString str = value.isString();
119        if (str == null) {
120            return null;
121        }
122        return str.stringValue();
123    }
124
125    /**
126     * Gets the CSV line containing the alias.<p>
127     *
128     * @return the CSV line
129     */
130    public String getLine() {
131
132        return m_line;
133    }
134
135    /**
136     * Gets the import message.<p>
137     *
138     * @return the import message
139     */
140    public String getMessage() {
141
142        return m_message;
143    }
144
145    /**
146     * Gets the import status.<p>
147     *
148     * @return the import status
149     */
150    public CmsAliasImportStatus getStatus() {
151
152        return m_status;
153    }
154}