* #%L
 * Alfresco Remote API
 * %%
 * Copyright (C) 2005 - 2016 Alfresco Software Limited
 * %%
 * This file is part of the Alfresco software. 
 * If the software was purchased under a paid Alfresco license, the terms of 
 * the paid license agreement will prevail.  Otherwise, the software is 
 * provided under the following open source license terms:
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
 * #L%

package org.alfresco.repo.web.scripts;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.alfresco.repo.nodelocator.CompanyHomeNodeLocator;
import org.alfresco.repo.nodelocator.NodeLocatorService;
import org.alfresco.repo.nodelocator.SharedHomeNodeLocator;
import org.alfresco.repo.nodelocator.SitesHomeNodeLocator;
import org.alfresco.repo.nodelocator.UserHomeNodeLocator;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.util.ISO8601DateFormat;
import org.json.JSONObject;
import org.springframework.extensions.surf.util.Content;
import org.springframework.extensions.webscripts.WebScriptRequest;

 * @author Nick Smith
 * @since 4.0
public class WebScriptUtil
    // General Keys
    public static final String DATA_KEY = "data";

    // NodeRef Keys
    public static final String STORE_PROTOCOL = "store_protocol";
    public static final String STORE_ID = "store_id";
    public static final String NODE_ID = "node_id";

    //Date/Calendar Keys
    public static final String DATE_TIME= "dateTime";
    public static final String FORMAT= "format";
    public static final String TIME_ZONE= "timeZone";
    public static final String ISO8601 = "ISO8601";

    public static String getContent(WebScriptRequest request) throws IOException
        Content content = request.getContent();
        return content.getContent();

    public static Map<String, Object> buildCalendarModel(Calendar calendar)
        Map<String, Object> model = buildDateModel(calendar.getTime());
        model.put(TIME_ZONE, calendar.getTimeZone().getID());
        return model;

    public static Map<String, Object> buildDateModel(Date dateTime)
        String dateStr = ISO8601DateFormat.format(dateTime);
        Map<String, Object> model = new HashMap<String, Object>();
        model.put(DATE_TIME, dateStr);
        model.put(FORMAT, ISO8601);
        return model;
    public static Calendar getCalendar(JSONObject json) throws ParseException
        Date date = getDate(json);
        if(date == null)
            return null;
        Calendar calendar = Calendar.getInstance();
        String timeZone = json.optString(TIME_ZONE);