
org.s1.mongodb.MongoDBFormat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of s1-mongodb Show documentation
Show all versions of s1-mongodb Show documentation
S1 MongoDB Log4j Appender + LogStorage impl
/*
* Copyright 2014 Grigory Pykhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.s1.mongodb;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.s1.S1SystemError;
import org.s1.misc.Closure;
import org.s1.objects.ObjectIterator;
import org.s1.objects.Objects;
import org.s1.table.format.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* MongoDB Format
*/
public class MongoDBFormat {
/**
*
* @param obj
* @return
*/
public static Map toMap(DBObject obj){
if(obj==null)
return null;
Map m = obj.toMap();
m.remove("_id");
/*if(m.get("_id")!=null){
m.put("id",m.get("_id").toString());
m.remove("_id");
}*/
m = Objects.iterate(m, new Closure() {
@Override
public Object call(ObjectIterator.IterateBean input) {
Object o = input.getValue();
if(o instanceof Map){
Map m = (Map)o;
if(m.containsKey("$date") && m.size()==1){
o = m.get("$date");
}
if(m.containsKey("_serializable") && m.size()==1){
try {
byte [] b = (byte [])m.get("_serializable");
ByteArrayInputStream bin = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bin);
o = ois.readObject();
} catch (Exception e) {
throw S1SystemError.wrap(e);
}
}
}
return o;
}
});
return m;
}
/**
*
* @param m
* @return
*/
public static DBObject fromMap(Map m){
if(m==null)
return null;
m = Objects.iterate(m, new Closure() {
@Override
public Object call(ObjectIterator.IterateBean input) {
Object o = input.getValue();
if (o instanceof BigInteger) {
o = Objects.cast(o, Long.class);
} else if (o instanceof BigDecimal) {
o = Objects.cast(o, Double.class);
} else if (o instanceof Float) {
o = Objects.cast(o, Double.class);
} else if(!(o instanceof List)
&& !(o instanceof Map)
&& !(o instanceof String)
&& !(o instanceof Boolean)
&& !(o instanceof Date)
&& !(o instanceof Number)
&& !(o instanceof ObjectId)
&& !(o instanceof byte[])){
if(o instanceof Serializable){
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
o = Objects.newHashMap("_serializable",bos.toByteArray());
} catch (Exception e) {
throw S1SystemError.wrap(e);
}
}
}
return o;
}
});
DBObject obj = new BasicDBObject(m);
/*if(m.get("id")!=null){
obj.put("_id",parseId(""+m.get("id")));
obj.removeField("id");
}*/
return obj;
}
/**
*
* @param query
* @return
*/
public static Map formatSearch(Query query){
Map o = Objects.newHashMap();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy