
de.taimos.dvalin.mongo.links.DLinkDAO Maven / Gradle / Ivy
package de.taimos.dvalin.mongo.links;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import org.bson.types.ObjectId;
import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import de.taimos.dvalin.mongo.JongoFactory;
/*
* #%L
* Spring DAO Mongo
* %%
* Copyright (C) 2013 - 2015 Taimos GmbH
* %%
* 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.
* #L%
*/
@Repository
public class DLinkDAO implements IDLinkDAO {
@Autowired
private MongoClient mongo;
private Jongo jongo;
@PostConstruct
public final void init() {
String dbName = System.getProperty("mongodb.name");
if (dbName == null) {
throw new RuntimeException("Missing database name; Set system property 'mongodb.name'");
}
DB db = this.mongo.getDB(dbName);
this.jongo = JongoFactory.createDefault(db);
}
@Override
public > T resolve(DocumentLink link) {
MongoCollection collection = this.jongo.getCollection(link.getTargetClass().getSimpleName());
return collection.findOne(new ObjectId(link.getObjectId())).as(link.getTargetClass());
}
@Override
public > List resolve(List> links, Class targetClass) {
MongoCollection collection = this.jongo.getCollection(targetClass.getSimpleName());
List ids = new ArrayList<>();
for (DocumentLink link : links) {
if (!link.getTargetClass().equals(targetClass)) {
throw new IllegalArgumentException("Invalid link in collection");
}
ids.add(new ObjectId(link.getObjectId()));
}
Iterator it = collection.find("{\"_id\" : {\"$in\" : #}}", ids).as(targetClass).iterator();
List resolved = new ArrayList<>();
while (it.hasNext()) {
resolved.add(it.next());
}
return resolved;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy