2017-02-16 16:36:22 +08:00
|
|
|
package cn.cloudowr.dict;
|
|
|
|
|
|
|
|
|
|
import com.jfinal.aop.Before;
|
|
|
|
|
import com.jfinal.render.ContentType;
|
|
|
|
|
import com.mongodb.client.MongoCollection;
|
|
|
|
|
import com.mongodb.client.MongoDatabase;
|
|
|
|
|
import com.mongodb.client.model.Filters;
|
|
|
|
|
import com.mongodb.client.model.UpdateOptions;
|
|
|
|
|
import com.mongodb.client.model.Updates;
|
|
|
|
|
import com.mongodb.client.result.DeleteResult;
|
|
|
|
|
import com.mongodb.client.result.UpdateResult;
|
|
|
|
|
import org.bson.Document;
|
|
|
|
|
import org.bson.conversions.Bson;
|
|
|
|
|
import org.bson.types.ObjectId;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by lyf66 on 2017/2/16.
|
|
|
|
|
*/
|
|
|
|
|
public class Controller extends com.jfinal.core.Controller{
|
|
|
|
|
public void index() {
|
|
|
|
|
renderText("2");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void dict() {
|
2017-02-16 17:26:12 +08:00
|
|
|
Map<String, String[]> paraMap = getParaMap();
|
|
|
|
|
List<Bson> ops = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
paraMap.forEach((key, arr) -> {
|
|
|
|
|
if ("id".equals(key)) {
|
|
|
|
|
ops.add(Filters.eq("_id", new ObjectId(arr[0])));
|
|
|
|
|
} else {
|
|
|
|
|
ops.add(Filters.eq(key, arr[0]));
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-02-16 16:36:22 +08:00
|
|
|
|
|
|
|
|
MongoCollection<Document> collection = getCollection();
|
2017-02-16 17:26:12 +08:00
|
|
|
List<Document> documents = new ArrayList<>();
|
|
|
|
|
collection.find(
|
|
|
|
|
Filters.and(ops)
|
|
|
|
|
).into(documents);
|
|
|
|
|
|
|
|
|
|
List<String> json = new ArrayList<>();
|
|
|
|
|
documents.forEach(doc -> {
|
|
|
|
|
json.add(doc.toJson());
|
|
|
|
|
});
|
|
|
|
|
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
2017-02-16 16:36:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void dictList() {
|
|
|
|
|
int pageNum = getParaToInt("pageNum", 0);
|
|
|
|
|
int pageSize = getParaToInt("pageSize", 20);
|
|
|
|
|
|
|
|
|
|
MongoCollection<Document> collection = getCollection();
|
|
|
|
|
List<Document> documents = new ArrayList<>();
|
|
|
|
|
collection.find().limit(pageSize).skip(pageNum * pageSize).into(documents);
|
|
|
|
|
|
|
|
|
|
List<String> json = new ArrayList<>();
|
|
|
|
|
documents.forEach(doc -> {
|
|
|
|
|
json.add(doc.toJson());
|
|
|
|
|
});
|
|
|
|
|
renderText(Arrays.toString(json.toArray()), ContentType.JSON);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addDict() {
|
|
|
|
|
Map<String, String[]> paraMap = getParaMap();
|
|
|
|
|
Document dict = new Document();
|
|
|
|
|
paraMap.forEach((key, arr) -> {
|
|
|
|
|
dict.put(key, arr[0]);
|
|
|
|
|
});
|
|
|
|
|
getCollection().insertOne(dict);
|
2017-02-16 16:57:45 +08:00
|
|
|
renderJson(dict.toJson());
|
2017-02-16 16:36:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before(IdInterceptor.class)
|
|
|
|
|
public void updateDict() {
|
|
|
|
|
String id = getPara("id");
|
|
|
|
|
String action = getPara("action");
|
|
|
|
|
String[] keysToUnset = getParaValues("keys");
|
|
|
|
|
List<Bson> ops = new ArrayList<>();
|
|
|
|
|
|
2017-02-16 17:13:15 +08:00
|
|
|
if ("unset".equals(action)) {
|
2017-02-16 16:36:22 +08:00
|
|
|
for (String key : keysToUnset) {
|
|
|
|
|
if (key.equals("id")) continue;
|
|
|
|
|
ops.add(Updates.unset(key));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Map<String, String[]> paraMap = getParaMap();
|
|
|
|
|
|
|
|
|
|
paraMap.forEach((key, arr) -> {
|
2017-02-16 17:13:15 +08:00
|
|
|
if (!"id".equals(key) && !"action".equals(key)) {
|
2017-02-16 16:36:22 +08:00
|
|
|
ops.add(Updates.set(key, arr[0]));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateResult result = getCollection().updateOne(
|
|
|
|
|
Filters.eq("_id", new ObjectId(id)),
|
|
|
|
|
Updates.combine(
|
|
|
|
|
ops
|
|
|
|
|
),
|
|
|
|
|
new UpdateOptions().upsert(true)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
renderJson("result", result.getMatchedCount() >= 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before(IdInterceptor.class)
|
|
|
|
|
public void deleteDict() {
|
|
|
|
|
String id = getPara("id");
|
|
|
|
|
|
|
|
|
|
DeleteResult result = getCollection().deleteOne(
|
|
|
|
|
Filters.eq("_id", new ObjectId(id))
|
|
|
|
|
);
|
|
|
|
|
renderJson("result", result.getDeletedCount() == 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MongoCollection<Document> getCollection() {
|
|
|
|
|
MongoDatabase mongoDataBase = Config.getMongoDataBase();
|
|
|
|
|
MongoCollection<Document> collection = mongoDataBase.getCollection("dict");
|
|
|
|
|
return collection;
|
|
|
|
|
}
|
|
|
|
|
}
|