Guest User

Untitled

a guest
Feb 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. @Document(collection = CitizenForumMessageDocument.COLLECTION_NAME)
  2. public class ImageDocument {
  3.  
  4. public static final String COLLECTION_NAME = "images";
  5.  
  6. @Id
  7. private String id; // autogenerated
  8. private Image data; // data for the client (web, mobile...)
  9. private ImageMeta meta; // for internal application work (uploader ip, etc...)
  10.  
  11. [...] // getter, setter
  12.  
  13. }
  14.  
  15. // send as is to a client
  16. public class Image {
  17.  
  18. private String id;
  19.  
  20. [...]
  21. }
  22.  
  23. public void saveUploadedImage(Client client, ImageForm form) {
  24.  
  25. ImageDocument doc = new ImageDocument();
  26.  
  27. dao.save(doc); // create document cause we need an id...
  28.  
  29. try {
  30. doc.setImage(createImage(form, doc.getId()));
  31. doc.setMeta(createMeta(client, form));
  32. } catch(Exception e){
  33. dao.remove(doc);
  34. return; // ugly...
  35. }
  36.  
  37. dao.update(doc);
  38. }
  39.  
  40. @Component
  41. public class MongoListener extends AbstractMongoEventListener<ImageDocument>
  42. {
  43.  
  44. private final MongoTemplate mongoTemplate;
  45.  
  46. @Autowired
  47. public MongoListener(final MongoTemplate mongoTemplate) {
  48. this.mongoTemplate = mongoTemplate;
  49. }
  50.  
  51. @Override
  52. public void onAfterSave(AfterSaveEvent<ImageDocument> event) {
  53.  
  54. ImageDocument imageDocument = event.getSource();
  55. if(imageDocument.getData().getId() == null) {
  56. imageDocument.getData().setId(imageDocument.getId());
  57. mongoTemplate.save(imageDocument);
  58. }
  59. }
  60. }
Add Comment
Please, Sign In to add comment