Allow exporting all data in bson, json, or csv formats.

This commit is contained in:
MarkBryanMilligan
2022-01-29 18:25:19 -06:00
parent eeec6cc697
commit eaf1e4504f
117 changed files with 41205 additions and 10527 deletions

View File

@@ -607,29 +607,43 @@ public class CollectionUtils {
return ret;
}
public static <T, V extends Comparable<V>> Map<V, T> transformToSortedMap(Collection<T> _coll, ITransformer<? super T, V> _transformer) {
return transformToMap(_coll, _transformer, new TreeMap<>());
}
public static <T, V> Map<V, T> transformToMap(Collection<T> _coll, ITransformer<? super T, V> _transformer) {
Map<V, T> mapValues = new HashMap<>();
return transformToMap(_coll, _transformer, new HashMap<>());
}
public static <T, V> Map<V, T> transformToMap(Collection<T> _coll, ITransformer<? super T, V> _transformer, Map<V, T> _map) {
if ((_coll == null) || (_transformer == null))
return mapValues;
return _map;
for (T t : _coll) {
V v = _transformer.transform(t);
if (v != null)
mapValues.put(v, t);
_map.put(v, t);
}
return mapValues;
return _map;
}
public static <T, V extends Comparable<V>, U> Map<V, U> transformToSortedMap(Collection<T> _coll, ITransformer<? super T, V> _keyTrans, ITransformer<? super T, U> _valTrans) {
return transformToMap(_coll, _keyTrans, _valTrans, new TreeMap<>());
}
public static <T, V, U> Map<V, U> transformToMap(Collection<T> _coll, ITransformer<? super T, V> _keyTrans, ITransformer<? super T, U> _valTrans) {
Map<V, U> mapValues = new HashMap<>();
return transformToMap(_coll, _keyTrans, _valTrans, new HashMap<>());
}
public static <T, V, U> Map<V, U> transformToMap(Collection<T> _coll, ITransformer<? super T, V> _keyTrans, ITransformer<? super T, U> _valTrans, Map<V, U> _map) {
if ((_coll == null) || (_keyTrans == null) || (_valTrans == null))
return mapValues;
return _map;
for (T t : _coll) {
V v = _keyTrans.transform(t);
U u = _valTrans.transform(t);
if ((v != null) && (u != null))
mapValues.put(v, u);
_map.put(v, u);
}
return mapValues;
return _map;
}
public static <T, V> Map<V, List<T>> transformToMultiMap(Collection<T> _coll, ITransformer<? super T, V> _transformer) {

View File

@@ -0,0 +1,32 @@
package com.lanternsoftware.util;
import java.util.Date;
public class DateRange {
private Date start;
private Date end;
public DateRange() {
}
public DateRange(Date _start, Date _end) {
start = _start;
end = _end;
}
public Date getStart() {
return start;
}
public void setStart(Date _start) {
start = _start;
}
public Date getEnd() {
return end;
}
public void setEnd(Date _end) {
end = _end;
}
}

View File

@@ -1,10 +0,0 @@
package com.lanternsoftware.util;
public abstract class LanternFiles {
public static final String SOURCE_PATH = "C:\\lantern\\LanternPowerMonitor\\";
public static final String OPS_PATH = "/opt/tomcat/";
// public static final String OPS_PATH = "D:\\zwave\\prodremote\\";
// public static final String OPS_PATH = "D:\\zwave\\localhost\\";
public static final String BACKUP_SOURCE = "D:\\zwave\\prodremote\\";
public static final String BACKUP_DEST = "D:\\zwave\\localhost\\";
}

View File

@@ -116,6 +116,8 @@ public abstract class ResourceLoader {
public static void writeFile(String _sFile, byte[] _btData) {
FileOutputStream os = null;
try {
int idx = _sFile.lastIndexOf(File.separator);
new File((idx > 0)?_sFile.substring(0, idx):_sFile).mkdirs();
os = new FileOutputStream(_sFile, false);
os.write(_btData);
os.flush();

View File

@@ -3,6 +3,7 @@ package com.lanternsoftware.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@@ -19,7 +20,7 @@ public abstract class ZipUtils {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream stream = null;
try {
stream = new GZIPOutputStream(out);
stream = new GZIPOutputStream(out){{def.setLevel(Deflater.BEST_SPEED);}};
stream.write(_btData);
IOUtils.closeQuietly(stream);
return out.toByteArray();

View File

@@ -1,6 +1,5 @@
package com.lanternsoftware.util.cryptography;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.LongBuffer;
@@ -17,7 +16,7 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import com.lanternsoftware.util.LanternFiles;
import com.lanternsoftware.util.external.LanternFiles;
import com.lanternsoftware.util.ResourceLoader;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
@@ -79,7 +78,7 @@ public class AESTool {
}
public static AESTool authTool() {
return new AESTool(ResourceLoader.loadFile(LanternFiles.OPS_PATH + "authKey.dat"));
return new AESTool(ResourceLoader.loadFile(LanternFiles.CONFIG_PATH + "authKey.dat"));
}
/**

View File

@@ -0,0 +1,10 @@
package com.lanternsoftware.util.external;
public class DevFiles {
public static void init() {
LanternFiles.SOURCE_CODE_PATH = "C:\\lantern\\LanternPowerMonitor\\";
LanternFiles.CONFIG_PATH = "D:\\zwave\\localhost\\";
LanternFiles.BACKUP_DEST_PATH = "D:\\zwave\\localhost\\";
LanternFiles.runOpsTasks = false;
}
}

View File

@@ -0,0 +1,12 @@
package com.lanternsoftware.util.external;
public abstract class LanternFiles {
public static String SOURCE_CODE_PATH;
public static String CONFIG_PATH;
public static String BACKUP_DEST_PATH;
public static boolean runOpsTasks;
static {
ProdConsoleFiles.init();
}
}

View File

@@ -0,0 +1,10 @@
package com.lanternsoftware.util.external;
public class ProdConsoleFiles {
public static void init() {
LanternFiles.SOURCE_CODE_PATH = "C:\\lantern\\LanternPowerMonitor\\";
LanternFiles.CONFIG_PATH = "/opt/tomcat/";
LanternFiles.BACKUP_DEST_PATH = "/home/backup/";
LanternFiles.runOpsTasks = false;
}
}

View File

@@ -0,0 +1,10 @@
package com.lanternsoftware.util.external;
public class ProdFiles {
public static void init() {
LanternFiles.SOURCE_CODE_PATH = "C:\\lantern\\LanternPowerMonitor\\";
LanternFiles.CONFIG_PATH = "/opt/tomcat/";
LanternFiles.BACKUP_DEST_PATH = "/home/backup/";
LanternFiles.runOpsTasks = true;
}
}

View File

@@ -0,0 +1,10 @@
package com.lanternsoftware.util.external;
public class ProdSupportFiles {
public static void init() {
LanternFiles.SOURCE_CODE_PATH = "C:\\lantern\\LanternPowerMonitor\\";
LanternFiles.CONFIG_PATH = "D:\\zwave\\prodremote\\";
LanternFiles.BACKUP_DEST_PATH = "D:\\zwave\\localhost\\";
LanternFiles.runOpsTasks = false;
}
}

View File

@@ -1,5 +1,42 @@
package com.lanternsoftware.util.dao.mongo;
import com.lanternsoftware.util.CollectionUtils;
import com.lanternsoftware.util.ITransformer;
import com.lanternsoftware.util.NullUtils;
import com.lanternsoftware.util.cryptography.RSAUtils;
import com.lanternsoftware.util.dao.AbstractDaoProxy;
import com.lanternsoftware.util.dao.DaoEntity;
import com.lanternsoftware.util.dao.DaoProxyType;
import com.lanternsoftware.util.dao.DaoQuery;
import com.lanternsoftware.util.dao.DaoSerializer;
import com.lanternsoftware.util.dao.DaoSort;
import com.lanternsoftware.util.dao.DaoSortField;
import com.lanternsoftware.util.dao.annotations.PrimaryKey;
import com.lanternsoftware.util.hash.MD5HashTool;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.DeleteOneModel;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -14,46 +51,6 @@ import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import com.lanternsoftware.util.dao.AbstractDaoProxy;
import com.lanternsoftware.util.dao.DaoEntity;
import com.lanternsoftware.util.dao.DaoProxyType;
import com.lanternsoftware.util.dao.DaoQuery;
import com.lanternsoftware.util.dao.DaoSerializer;
import com.lanternsoftware.util.dao.DaoSort;
import com.lanternsoftware.util.dao.DaoSortField;
import com.lanternsoftware.util.dao.annotations.PrimaryKey;
import com.mongodb.bulk.BulkWriteResult;
import com.mongodb.client.model.DeleteOneModel;
import com.mongodb.client.model.ReplaceOptions;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lanternsoftware.util.CollectionUtils;
import com.lanternsoftware.util.ITransformer;
import com.lanternsoftware.util.NullUtils;
import com.lanternsoftware.util.cryptography.RSAUtils;
import com.lanternsoftware.util.hash.MD5HashTool;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
public class MongoProxy extends AbstractDaoProxy {
private static final Logger LOG = LoggerFactory.getLogger(MongoProxy.class);
private final MongoClient client;
@@ -146,6 +143,15 @@ public class MongoProxy extends AbstractDaoProxy {
}
public List<DaoEntity> queryForEntities(String _tableName, final String _primaryKey, DaoQuery _query, Collection<String> _fields, DaoSort _sort, int _offset, int _count) {
return CollectionUtils.transform(queryIterator(_tableName, _primaryKey, _query, _fields, _sort, _offset, _count), DaoEntity::new);
}
public <T> Iterable<T> queryIterator(Class<T> _class, DaoQuery _query, Collection<String> _fields, DaoSort _sort, int _offset, int _count) {
String pk = CollectionUtils.getFirst(DaoSerializer.getFieldsByAnnotation(_class, PrimaryKey.class));
return queryIterator(DaoSerializer.getTableName(_class, getType()), pk, _query, _fields, _sort, _offset, _count).map(_d->DaoSerializer.fromDaoEntity(new DaoEntity(_d), _class));
}
public FindIterable<Document> queryIterator(String _tableName, final String _primaryKey, DaoQuery _query, Collection<String> _fields, DaoSort _sort, int _offset, int _count) {
final String pk = NullUtils.isEmpty(_primaryKey) ? "_id" : _primaryKey;
FindIterable<Document> iter;
if (_query != null) {
@@ -214,7 +220,7 @@ public class MongoProxy extends AbstractDaoProxy {
iter.skip(_offset);
if (_count > 0)
iter.limit(_count);
return CollectionUtils.transform(iter, DaoEntity::new);
return iter;
}
@Override

View File

@@ -54,6 +54,15 @@ import com.lanternsoftware.util.dao.annotations.PrimaryKey;
public class DaoSerializer {
private static final Logger LOG = LoggerFactory.getLogger(DaoSerializer.class);
private static final Map<Class<?>, List<IDaoSerializer>> serializers = new HashMap<>();
public static final JsonWriterSettings JSON_PRETTY_SETTINGS = JsonWriterSettings.builder().int64Converter((Long _long, StrictJsonWriter _writer)->{
if (_long != null)
_writer.writeNumber(_long.toString());
}).indent(true).build();
public static final JsonWriterSettings JSON_COMPACT_SETTINGS = JsonWriterSettings.builder().int64Converter((Long _long, StrictJsonWriter _writer)->{
if (_long != null)
_writer.writeNumber(_long.toString());
}).indent(false).build();
static {
for (IDaoSerializer serializer : ServiceLoader.load(IDaoSerializer.class)) {
@@ -831,14 +840,7 @@ public class DaoSerializer {
Document doc = _e.toDocument();
if (_removeNulls)
removeNulls(doc.values());
JsonWriterSettings.Builder settings = JsonWriterSettings.builder().int64Converter(new Converter<Long>() {
@Override
public void convert(Long _long, StrictJsonWriter _writer) {
if (_long != null)
_writer.writeNumber(_long.toString());
}
});
return doc.toJson(settings.indent(_pretty).build());
return doc.toJson(_pretty?JSON_PRETTY_SETTINGS:JSON_COMPACT_SETTINGS);
}
}
catch (Exception e) {

View File

@@ -30,10 +30,6 @@ public abstract class FreemarkerServlet extends LanternServlet {
return path;
}
public static void redirect(HttpServletResponse _response, String _sURL) throws IOException {
_response.sendRedirect(_response.encodeRedirectURL(_sURL));
}
public void render(HttpServletResponse _rep, String _sHtmlResourceKey, Map<String, Object> _mapModel) {
String html = FreemarkerUtil.render(getFreemarkerConfig(), _sHtmlResourceKey, _mapModel);
if (html == null)

View File

@@ -10,6 +10,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -83,6 +84,15 @@ public abstract class LanternServlet extends HttpServlet {
}
}
public void redirect(HttpServletResponse _response, String _sURL) {
try {
_response.sendRedirect(_response.encodeRedirectURL(_sURL));
}
catch (IOException _e) {
_response.setStatus(500);
}
}
protected DaoEntity getRequestZipBson(HttpServletRequest _req) {
return DaoSerializer.fromZipBson(getRequestPayload(_req));
}