@Override public void prePersist(final Object entity, final DBObject dbObj, final Mapper mapper) { MappedClass mclass = mapper.getMappedClass(entity); Field id = mclass.getIdField(); if (id != null && id.getAnnotation(GeneratedValue.class) != null) { try { id.setAccessible(true); final String collName = gen.value(mclass.getClazz()); final Query<StoredId> q = db.find(StoredId.class, "_id", collName); final UpdateOperations<StoredId> uOps = db.createUpdateOperations(StoredId.class) .inc("value"); StoredId newId = db.findAndModify(q, uOps); if (newId == null) { newId = new StoredId(collName); db.save(newId); } id.set(entity, newId.value); } catch (Exception ex) { throw new IllegalStateException("Can't generate ID on " + mclass, ex); } } }
/** * query mongod entity * * @param cls entity class * @param <T> paramter object cls * @return Query https://github.com/mongodb/morphia/wiki/Query */ public static <T> Query<T> query(Class<T> cls) { return _datastore.find(cls); }
@Override public Long count() { return datastore.find(BookingImpl.class).countAll(); } }
public static Seq next(String name) { Datastore ds = MorphiaPlugin.ds(); Query<Seq> q = ds.find(Seq.class, "_id", name); UpdateOperations<Seq> o = ds.createUpdateOperations(Seq.class).inc("value"); Seq newId = ds.findAndModify(q, o); if (null == newId) { newId = new Seq(name); ds.save(newId); } return newId; }
@Override public Long count() { return datastore.find(CustomerImpl.class).countAll(); }
public int assignNextBuildNumber(final DynamicProject project) { final Datastore datastore = getDatastore(); BuildNumberCounter seq = datastore.findAndModify( datastore.find(BuildNumberCounter.class, "key = ", project.getFullName()), // query datastore.createUpdateOperations(BuildNumberCounter.class).inc("counter") // update ); if (seq == null) { seq = new BuildNumberCounter(project.getFullName(), 1); datastore.save(seq); } return seq.getCounter(); }
public Optional<EntityDiscussionThread> getThread(@Nonnull ThreadId id) { return Optional.ofNullable(datastore.find(EntityDiscussionThread.class) .field("_id").equal(id) .get()); }
MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost")); Morphia morphia = new Morphia(); morphia.map(LookupData.class); //lookupdata collection is under my local db "tutorials" in this case Datastore datastore = morphia.createDatastore(mongoClient, "tutorials"); Map<String,ArrayList> categotyLookUpMap = new HashMap<String,ArrayList>(); LookupData lookupData = datastore.find(LookupData.class).get(); categotyLookUpMap.put(lookupData.getKey(), lookupData.getValue());
@Override public Booking getBooking(String user, String bookingId) { try{ Query<BookingImpl> q = datastore.find(BookingImpl.class).field("_id").equal(bookingId); Booking booking = q.get(); return booking; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public List<SlackWebhook> getWebhooks(@Nonnull ProjectId projectId) { return datastore.find(SlackWebhook.class).field(PROJECT_ID).equal(projectId).asList(); }
@Nonnull public PrefixDeclarations find(@Nonnull ProjectId projectId) { PrefixDeclarations prefixDeclarations = datastore.find(PrefixDeclarations.class) .field(PROJECT_ID).equal(projectId) .get(); if(prefixDeclarations == null) { return PrefixDeclarations.get(projectId); } else { return prefixDeclarations; } } }
@Override public void invalidateSession(String sessionid) { Query<CustomerSessionImpl> q = datastore.find(CustomerSessionImpl.class).field("_id").equal(sessionid); datastore.delete(q); }
@Override protected CustomerSession getSession(String sessionid){ Query<CustomerSessionImpl> q = datastore.find(CustomerSessionImpl.class).field("_id").equal(sessionid); return q.get(); }
@Override public Model findById(Object id) { if (id == null) return null; try { return ds().find(clazz, keyName(), Binder.directBind(id.toString(), keyType())).get(); } catch (Exception e) { // Key is invalid, thus nothing was found warn(e, "cannot find entity[%s] with id: %s", clazz.getName(), id); return null; } }
@Override public List<ProjectWebhook> getProjectWebhooks(@Nonnull ProjectId projectId, ProjectWebhookEventType event) { return datastore.find(ProjectWebhook.class) .field(PROJECT_ID).equal(projectId) .field(SUBSCRIBED_TO_EVENTS).equal(event) .asList(); } }
@Override public Customer getCustomerByUsername(String username) { Query<CustomerImpl> q = datastore.find(CustomerImpl.class).field("_id").equal(username); Customer customer = q.get(); if (customer != null) { customer.setPassword(null); } return customer; }
public List<EntityDiscussionThread> findThreads(@Nonnull ProjectId projectId, @Nonnull OWLEntity entity) { datastore.createQuery(EntityDiscussionThread.class); return datastore.find(EntityDiscussionThread.class) .disableValidation() .field(PROJECT_ID).equal(projectId) .field(ENTITY).equal(entity) .order("-comments.0.createdAt") .asList(); }
@Override protected FlightSegment getFlightSegment(String fromAirport, String toAirport){ Query<FlightSegmentImpl> q = datastore.find(FlightSegmentImpl.class).field("originPort").equal(fromAirport).field("destPort").equal(toAirport); FlightSegment segment = q.get(); if (segment == null) { segment = new FlightSegmentImpl(); // put a sentinel value of a non-populated flightsegment } return segment; }
public void replaceEntity(ProjectId projectId, OWLEntity entity, OWLEntity withEntity) { Query<EntityDiscussionThread> query = datastore.find(EntityDiscussionThread.class) .field(PROJECT_ID).equal(projectId) .field(ENTITY).equal(entity); UpdateOperations<EntityDiscussionThread> updateOperations = datastore.createUpdateOperations(EntityDiscussionThread.class); updateOperations.set("entity", withEntity); datastore.update(query, updateOperations); }