Files
alfresco-community-repo/source/java/org/alfresco/repo/web/scripts/WebScriptUtil.java
Ramona Neamtu 3ff3ba22c7 SHA-1867 : Unable to create links in Shared Files / My Files root node from the Search Results page
- Added method WebScriptUtil.resolveNodeReference() which will resolve nodeRef

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@132544 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-11-08 11:47:03 +00:00

185 lines
6.0 KiB
Java

/*
* #%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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 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);
if(timeZone != null)
{
TimeZone zone = TimeZone.getTimeZone(timeZone);
calendar.setTimeZone(zone);
}
calendar.setTime(date);
return calendar;
}
public static Date getDate(JSONObject json) throws ParseException
{
if(json == null)
{
return null;
}
String dateTime = json.optString(DATE_TIME);
if(dateTime == null)
{
return null;
}
String format = json.optString(FORMAT);
if(format!= null && ISO8601.equals(format) == false)
{
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.parse(dateTime);
}
return ISO8601DateFormat.parse(dateTime);
}
public static Map<String, Object> createBaseModel(Map<String, Object> result)
{
Map<String, Object> model = new HashMap<String, Object>();
model.put(DATA_KEY, result);
return model;
}
public static Map<String, Object> createBaseModel(List<Map<String, Object>> results)
{
Map<String, Object> model = new HashMap<String, Object>();
model.put(DATA_KEY, results);
return model;
}
public static NodeRef getNodeRef(Map<String, String> params)
{
String protocol = params.get(STORE_PROTOCOL);
String storeId= params.get(STORE_ID);
String nodeId= params.get(NODE_ID);
if(protocol == null || storeId == null || nodeId==null )
{
return null;
}
return new NodeRef(protocol, storeId, nodeId);
}
public static NodeRef resolveNodeReference(String reference, NodeLocatorService nodeLocatorService)
{
NodeRef nodeRef = null;
switch (reference)
{
case "alfresco://company/home":
nodeRef = nodeLocatorService.getNode(CompanyHomeNodeLocator.NAME, null, null);
break;
case "alfresco://user/home":
nodeRef = nodeLocatorService.getNode(UserHomeNodeLocator.NAME, null, null);
break;
case "alfresco://company/shared":
nodeRef = nodeLocatorService.getNode(SharedHomeNodeLocator.NAME, null, null);
break;
case "alfresco://sites/home":
nodeRef = nodeLocatorService.getNode(SitesHomeNodeLocator.NAME, null, null);
break;
default:
nodeRef = new NodeRef(reference);
break;
}
return nodeRef;
}
}