Notes
![]() ![]() Notes - notes.io |
package com.manageengine.ela.server.common.client.datasource;
import com.adventnet.la.util.DateFormatUtil;
import com.manageengine.ela.server.common.client.structure.ClientTableStructure;
import com.manageengine.ela.server.common.client.structure.TableUniqueIdentifier;
import com.manageengine.ela.server.common.data.conversion.ConversionConstants;
import com.manageengine.ela.server.common.data.conversion.DataConverter;
import com.manageengine.ela.server.common.response.BaseStatusCode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
import java.util.logging.Logger;
/**
* @author praveen.aj
*
*/
public abstract class ClientAbstractFormatter<T>
{
private static final Logger LOGGER = Logger.getLogger(ClientAbstractFormatter.class.getName());
public ArrayList<T> getColumnList(TableUniqueIdentifier tableIdentifier) throws BaseStatusCode
{
return getAllColumns(tableIdentifier).get(ClientTableStructure.ORIGINAL_COLUMNS);
}
public Map<String,ArrayList<T>> getAllColumns(TableUniqueIdentifier tableIdentifier) throws BaseStatusCode
{
return getAllColumns(tableIdentifier,null);
}
public ArrayList<T> getColumnList(TableUniqueIdentifier tableIdentifier, ArrayList<T> allowedList) throws BaseStatusCode
{
return getAllColumns(tableIdentifier, allowedList).get(ClientTableStructure.ORIGINAL_COLUMNS);
}
public Map<String,ArrayList<T>> getAllColumns(TableUniqueIdentifier tableIdentifier, ArrayList<T> allowedList) throws BaseStatusCode
{
return getColumnListMap(tableIdentifier, null, allowedList);
}
private Map<String,ArrayList<T>> getColumnListMap(TableUniqueIdentifier tableIdentifier,ArrayList<T> skipList,ArrayList<T> allowedList) throws BaseStatusCode
{
JSONArray structure = ClientTableStructure.getColumnDefinition(tableIdentifier);
return collectColumnBasedOnCriteria(structure, skipList , allowedList);
}
protected Map<String,ArrayList<T>> collectColumnBasedOnCriteria(JSONArray columnDefinition, ArrayList<T> skipList,ArrayList<T> allowdList) throws BaseStatusCode
{
Map<String,ArrayList<T>> columnDetailsMap = new HashMap<>();
ArrayList<T> orgColumnList = new ArrayList<>();
ArrayList<T> psedoColumns = new ArrayList<>();
for (int index = 0; index < columnDefinition.length(); index++)
{
try
{
JSONObject singleObject = columnDefinition.getJSONObject(index);
// isPseudoColumn variable is used to mark the fields which are not present in the database but displayed in the ember table.
boolean isPseudoColumn = !singleObject.isNull("IS_PSEUDO") && singleObject.getBoolean("IS_PSEUDO"); // No I18N
if (!isValidColumn(singleObject))
{
continue;
}
T column = getNewColumn(singleObject);
if(!verifySkipListAndAllowedList(column, skipList, allowdList))
{
continue;
}
Long transformID = singleObject.isNull("TRANSFORM_ID") ? null : singleObject.getLong("TRANSFORM_ID"); //No I18N
if (transformID != null && checkAndAddtransformerCache(column,transformID)) //TODO: handle this
{
fillDependentColumns(transformID, orgColumnList);
}
if(isPseudoColumn)
{
psedoColumns.add(column);
}
else if(!orgColumnList.contains(column))
{
orgColumnList.add(column);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
columnDetailsMap.put(ClientTableStructure.ORIGINAL_COLUMNS, orgColumnList);
columnDetailsMap.put(ClientTableStructure.PSEUDO_COLUMNS, psedoColumns);
return columnDetailsMap;
}
public JSONArray getTransformedData(LinkedList<HashMap<T, Object>> table, Locale locale, TimeZone timeZone , String dateFormat) throws JSONException
{
JSONArray resultJsonArray = new JSONArray();
for (HashMap<T, Object> singleRowHashMap : table)
{
JSONObject rowJsonObject = transform(locale,timeZone,singleRowHashMap,dateFormat);
resultJsonArray.put(rowJsonObject);
}
return resultJsonArray;
}
public JSONArray getTransformedDataWithDummyValue(LinkedList<HashMap<T, Object>> table,List<T> dummy, Locale locale, TimeZone timeZone,String dateFormat) throws JSONException
{
JSONArray resultJsonArray = new JSONArray();
HashMap<T,Object> dummymap = new HashMap<>();
for(T column: dummy)
{
dummymap.put(column , null);
}
for (HashMap<T, Object> singleRowHashMap : table)
{
singleRowHashMap.putAll(dummymap);
JSONObject rowJsonObject = transform(locale,timeZone,singleRowHashMap,dateFormat);
resultJsonArray.put(rowJsonObject);
}
return resultJsonArray;
}
private JSONObject transform(Locale locale, TimeZone timeZone, HashMap<T, Object> rowHashMap,String dateFormat) throws JSONException
{
JSONObject rowJsonObject = new JSONObject();
Map<Object, Object> additionalArgs = new HashMap<Object,Object>();
additionalArgs.put(ConversionConstants.CLINET_TIMEZONE, timeZone);
additionalArgs.put(ConversionConstants.CLINET_LOCALE, locale);
additionalArgs.put(DateFormatUtil.DATE_FORMAT, dateFormat);
for (T key : rowHashMap.keySet())
{
String jsonKey = getCustomResultKeyName(key);
DataConverter<T, Object> convertr = getTransformer(key);
if(convertr != null)
{
try
{
rowJsonObject.put(jsonKey, convertr.transform(rowHashMap.get(key), rowHashMap , additionalArgs));
}
catch(Exception exp)
{
rowJsonObject.put(jsonKey, rowHashMap.get(key));
LOGGER.fine("Exception in data conversion : "+exp.getMessage());
}
}
else
{
rowJsonObject.put(jsonKey, rowHashMap.get(key));
}
}
return rowJsonObject;
}
protected abstract DataConverter<T, Object> getTransformer(T data);
protected abstract String getCustomResultKeyName(T data);
protected abstract boolean verifySkipListAndAllowedList(T column, ArrayList<T> skipList , ArrayList<T> allowdList);
protected abstract boolean checkAndAddtransformerCache(T data, Long transformerID);
protected abstract T getNewColumn(JSONObject singleObject) throws JSONException;
protected abstract boolean isValidColumn(JSONObject singleObject) throws JSONException;
protected abstract void fillDependentColumns(Long transformID,ArrayList<T> columnList) throws BaseStatusCode;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team