Skip to content

Instantly share code, notes, and snippets.

@aakash14goplani
Last active June 10, 2018 09:42
Show Gist options
  • Save aakash14goplani/7d0a7cf6a60a7610501ff9e1284e28e8 to your computer and use it in GitHub Desktop.
Save aakash14goplani/7d0a7cf6a60a7610501ff9e1284e28e8 to your computer and use it in GitHub Desktop.
Store asset information in JSON and load it using key value pair

What does this code do?

  1. Fetch all the details of asset using Asset API's read() method.
  2. Create JSON tree using JSON simple library and populate it with asset details from #1
  3. Print an JSON tree having all the asset details
<%@ taglib prefix="cs" uri="futuretense_cs/ftcs1_0.tld"
%><%@ taglib prefix="asset" uri="futuretense_cs/asset.tld"
%><%@ taglib prefix="ics" uri="futuretense_cs/ics.tld"
%><%@ taglib prefix="render" uri="futuretense_cs/render.tld"
%><%@ page import="COM.FutureTense.Interfaces.*, COM.FutureTense.Util.ftMessage, com.fatwire.assetapi.data.*, com.fatwire.assetapi.*, COM.FutureTense.Util.ftErrors, com.fatwire.system.*, com.fatwire.assetapi.data.*, com.fatwire.assetapi.query.*, com.openmarket.xcelerate.asset.AssetIdImpl, com.openmarket.xcelerate.asset.AssetList, com.fatwire.assetapi.def.AssetTypeDef, java.util.*, org.json.simple.JSONObject, org.json.simple.JSONArray, com.fatwire.assetapi.def.AttributeTypeEnum, java.net.URLEncoder"
%><%
 /******************************************************************************************************************************
   *    Element Name        :  GetAssetInfo 
   *    Author              :  Aakash Goplani 
   *    Creation Date       :  (06/09/2018) 
   *    Description         :  Element that read assets using API and writes details in JSON format
   *    Input Parameters    :  Variables required by this Element : assetId and assetType
   *    Output              :  JSON Object
 *****************************************************************************************************************************/
%><cs:ftcs>
	<%-- Record dependencies for the Template --%>
	<ics:if condition='<%=ics.GetVar("tid") != null%>'>
		<ics:then>
			<render:logdep cid='<%=ics.GetVar("tid")%>' c="Template" />
		</ics:then>
	</ics:if>
<%!public JSONObject createAssetJSONObject(String Asset){
	String assetId = Asset.substring(Asset.indexOf(":") + 1);
	String assetType = Asset.substring(0, Asset.indexOf(":"));
	JSONObject assetObject = new JSONObject();
	assetObject.put("AssetId",assetId);
	assetObject.put("AssetType",assetType);
	return assetObject;
}
%>
<%
	String assetId = ics.GetVar("assetId");
	String assetType = ics.GetVar("assetType");
		
	JSONObject jsonObject = new JSONObject();
	JSONObject jsonAttributeObject = new JSONObject();
	JSONArray jsonArrayList = null;
	
	try {
		Session sessionObject = SessionFactory.getSession();
		AssetDataManager assetDataManager = (AssetDataManager)sessionObject.getManager(AssetDataManager.class.getName());
			
		AssetId assetIdInstance = new AssetIdImpl(ics.GetVar("assetType"),Long.valueOf(ics.GetVar("assetId")));
		List<AssetId> assetIdList = new ArrayList<AssetId>();
		assetIdList.add(assetIdInstance);
		int count = 0;	
			
		Iterable<AssetData> assetDataList = assetDataManager.read(assetIdList);
		
		%><render:logdep c='<%=ics.GetVar("assetType") %>' cid='<%=ics.GetVar("assetId") %>' deptype="exact" /><%
		
		for(AssetData assetData : assetDataList){
			AssetDataImpl assetDataImpl = new AssetDataImpl(assetData.getAssetId());
			Iterable<String> associationNameList = assetDataImpl.getAssociationNames();
			for(String associationName : associationNameList){
				jsonArrayList = new JSONArray();
				List<AssetId> associatedAssetsList = assetDataImpl.getAssociatedAssets(associationName);
				for(AssetId associatedAssets : associatedAssetsList){
					jsonArrayList.add(createAssetJSONObject(associatedAssets.toString()));
				}
				jsonAttributeObject.put(associationName, jsonArrayList);
			}
			
			List<AttributeData> attributeDataList = new ArrayList<AttributeData>();
			List<String> multipleValues = new ArrayList<String>();
			attributeDataList = assetData.getAttributeData();
			
			if(!attributeDataList.isEmpty() && attributeDataList != null){
				for(AttributeData attributeData : attributeDataList){
					jsonArrayList = new JSONArray();
					/*
						Types of ENUM : 
						1. enumstring
						2. enumdate
						3. enumurl
						4. enumarray
						5. enumlong
						6. enumwebreference
						7. enumasset
						8. enumtext
					*/
					//check for enumasset of type [type:id]
					if(attributeData.getData()!=null && AttributeTypeEnum.ASSET.equals(attributeData.getType())){
						//check for enumasset that has list eg publist, webreference, seg-rating etc
						if(ArrayList.class.isInstance(attributeData.getData())) {
							ArrayList<AssetIdImpl> assetList = (ArrayList)attributeData.getData();
							
							for(AssetIdImpl assetIdImpl : assetList){
								jsonArrayList.add(createAssetJSONObject(assetIdImpl.toString()));
							}
							jsonAttributeObject.put(attributeData.getAttributeName(),jsonArrayList);
						
						} else {
							jsonAttributeObject.put(attributeData.getAttributeName(), createAssetJSONObject(attributeData.getData().toString()));
						}
					
					} else if(attributeData.getData()!=null && (AttributeTypeEnum.STRING.equals(attributeData.getType()) || 
						AttributeTypeEnum.LARGE_TEXT.equals(attributeData.getType()))){
						//check for enumstring (name, title, description etc) and enumtext (body_text : WYSIWYG)
						if (attributeData.getData() instanceof List) {
							// this one is for assets that accepts multiple values
							String temp = "";
							multipleValues = (List<String>)attributeData.getData();
							for(String singleValue : multipleValues) {
								temp = temp + URLEncoder.encode(singleValue, "UTF-8") + ",";
							}
							jsonAttributeObject.put(attributeData.getAttributeName(), temp);
						} else {
							jsonAttributeObject.put(attributeData.getAttributeName(), URLEncoder.encode((String)attributeData.getData(), "UTF-8"));
						}
					
					} else if(attributeData.getData()!=null && !AttributeTypeEnum.ARRAY.equals(attributeData.getType())){
						//check for enumarray eg publist, seg-rating etc
						jsonAttributeObject.put(attributeData.getAttributeName(),attributeData.getData().toString());
					
					} else{
						//for rest all enums
						jsonAttributeObject.put(attributeData.getAttributeName(), attributeData.getData());
					}
				}
			}
			if(jsonAttributeObject.get("template") != null && jsonAttributeObject.get("template").toString() != null);{
	
				%><asset:gettemplaterootelement template='<%=jsonAttributeObject.get("template").toString() %>' type='<%=assetType %>'
				output="templateRootElement" /><%
	
				jsonAttributeObject.put("templateRootElement",ics.GetVar("templateRootElement"));
			}
		}
		
		jsonObject.put("Attributes", jsonAttributeObject);
		jsonObject.put("Timestamp", ""+new Date() + "");
		jsonObject.put("AssetType", ics.GetVar("assetType"));
		out.println(jsonObject);
	}
	catch(Exception e){
		jsonAttributeObject.put("error_message",e.getMessage());
		jsonAttributeObject.put("assetId",ics.GetVar("assetId"));
		ics.LogMsg("Error : " + e.getMessage());
	}
%></cs:ftcs>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment