Package org.atomojo.app.db

Examples of org.atomojo.app.db.EntryMedia


            String ext = metaService.getExtension(baseMediaType);
            if (ext!=null) {
               file += "."+ext;
            }
         }
         EntryMedia media;
         try {
            media = entry.createResource(file,mediaType);
         } catch (SQLException ex) {
            try {
               entry.delete(null);
            } catch (SQLException ox) {
               log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
            }
            throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot create entry media resource in database.",ex);
         }
         if (media==null) {
            throw new AppException(Status.CLIENT_ERROR_BAD_REQUEST,"Media entry name "+file+" refused.");
         }

         // Get author name for identity
         String authorName = user.getName();

         // Create entry document
         Document doc = null;
         try {
            String title = URLDecoder.decode(slug,"UTF-8");
            doc = AtomResource.createMediaEntryDocument(title,entry.getUUID(),entry.getCreated(),authorName,file,mediaType);
         } catch (XMLException ex) {
            try {
               entry.delete(null);
            } catch (SQLException ox) {
               log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
            }
            throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot create media entry document.",ex);
         } catch (UnsupportedEncodingException ex) {
            try {
               entry.delete(null);
            } catch (SQLException ox) {
               log.log(Level.SEVERE,"Cannot delete entry for exception cleanup.",ox);
            }
            throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot decode slug for media entry title.",ex);
         }

         try {
            String path = feed.getPath();
            Status entryStatus = storage.storeEntry(path,feed.getUUID(),entry.getUUID(),doc);
            if (entryStatus.isSuccess()) {
               Status mediaStatus = storage.storeMedia(path,feed.getUUID(),media.getName(),mediaType,is);
               // TODO: storage may change media type parameters and the entry needs to be updated
               if (!mediaStatus.isSuccess()) {
                  try {
                     entry.delete(null);
                  } catch (SQLException ox) {
View Full Code Here


         }
      } catch (SQLException ex) {
         throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot categorize entry due to SQL Exception.",ex);
      }

      EntryMedia media = null;
      try {
         Iterator<EntryMedia> resources = entry.getResources();
         if (resources.hasNext()) {
            media = resources.next();
            while (resources.hasNext()) {
View Full Code Here

  
   public void updateMedia(Feed feed, String file, Representation entity)
      throws AppException
   {
      try {
         EntryMedia media = feed.findEntryResource(file);

         MediaType mediaType = entity.getMediaType();
         mediaType = MediaType.valueOf(mediaType.getName());
         media.setMediaType(mediaType);
         storage.storeMedia(feed.getPath(),feed.getUUID(),media.getName(),mediaType,entity.getStream());
         media.edited();
      } catch (IOException ex) {
         throw new AppException(Status.SERVER_ERROR_INTERNAL,"Cannot update entry media "+file,ex);
      } catch (SQLException ex) {
         throw new AppException(Status.SERVER_ERROR_INTERNAL,"Database error while updating entry media "+file,ex);
      }
View Full Code Here

               while (entries.hasNext()) {
                  Journal.UpdatedEntry updated = (Journal.UpdatedEntry)entries.next();
                  String name = updated.getOperation()==Journal.CREATE_OPERATION ? "created" : "updated";
                  Feed feed = updated.getFeed();
                  Entry entry = updated.getEntry();
                  EntryMedia media = updated.getResource();
                  out.write('<');
                  out.write(name);
                  out.write(" at='");
                  out.write(AtomResource.toXSDDate(updated.getOccurredOn()));
                  out.write("' feed='");
                  out.write(feed.getUUID().toString());
                  if (entry!=null) {
                     out.write("' entry='");
                     out.write(entry.getUUID().toString());
                  }
                  if (media!=null) {
                     out.write("' media='");
                     out.write(media.getName());
                  }
                  out.write("'/>\n");
               }
               out.write("</journal>");
               out.flush();
View Full Code Here

         Iterator<Entry> entries = feed.getEntries();
         while (entries.hasNext()) {
            Entry entry = entries.next();
            Iterator<EntryMedia> resources = entry.getResources();
            while (resources.hasNext()) {
               EntryMedia media = resources.next();
               Representation mediaRep = storage.getMedia(fpath,feed.getUUID(),media.getName());
               if (mediaRep==null) {
                  throw new IOException("Cannot get media "+media.getName()+" for feed at path "+fpath);
               }
               OutputStream mout = context.startMediaOutput(media);
               mediaRep.write(mout);
               context.endMediaOutput();
               mediaRep.release();
View Full Code Here

      dir.listFiles(new FileFilter() {
         public boolean accept(File file) {
            String name = file.getName();
            if (!file.isDirectory() && name.charAt(0)!='.') {
               try {
                  EntryMedia media = feed.findEntryResource(name);
                  if (media==null) {
                     importMedia(feed,file);
                  }
               } catch (Exception ex) {
                  getLogger().log(Level.SEVERE,"Error while importing media "+file.getAbsolutePath(),ex);
                  ok.set(false);
               }
            }
            return false;
         }
      });
      Iterator<org.atomojo.app.db.Feed> children = feed.getChildren();
      while (children.hasNext()) {
         org.atomojo.app.db.Feed child = children.next();
         File childDir = new File(dir,child.getName());
         if (!childDir.exists()) {
            child.delete();
         }
      }
      Iterator<Entry> entries = feed.getEntries();
      while (entries.hasNext()) {
         Entry entry = entries.next();
         File entryFile = new File(dir,"."+entry.getUUID()+".atom");
         boolean delete = false;
         if (entryFile.exists()) {
            Iterator<EntryMedia> resources = entry.getResources();
            while (!delete && resources.hasNext()) {
               File media = new File(dir,resources.next().getName());
               if (!media.exists()) {
                  delete = true;
               }
            }
         } else {
            delete = true;
         }
         if (delete) {
            entry.delete(new MediaEntryListener() {
               public void onDelete(EntryMedia resource) {
                  File media = new File(dir,resource.getName());
                  if (media.exists()) {
                     media.delete();
                  }
               }
            });
            if (entryFile.exists()) {
               entryFile.delete();
View Full Code Here

                           entry = feed.findEntry(entryId);
                        } catch (SQLException ex) {
                           log.log(Level.SEVERE,"Cannot find entry "+entryId+" due to exception.",ex);
                           errorCount++;
                        }
                        EntryMedia resource = null;
                        boolean hasMedia = false;
                        if (entry!=null) {
                           try {
                              Iterator<EntryMedia> resources = entry.getResources();
                              if (resources.hasNext()) {
                                 hasMedia = true;
                                 while (resources.hasNext()) {
                                    resource = resources.next();
                                    if (!resource.getName().equals(src)) {
                                       resource = null;
                                    }
                                 }
                              }
                           } catch (SQLException ex) {
                              log.log(Level.SEVERE,"Cannot enumerate entry "+index.getId()+" media due to exception.",ex);
                              errorCount++;
                           }
                        }
                        if (entry==null || (src!=null && !hasMedia) || (hasMedia && src==null)) {
                           if (entry!=null) {
                              // delete the entry because it changed to have a media or non-media content (rare)
                              try {
                                 app.deleteEntry(feed,entry);
                              } catch (AppException ex) {
                                 if (ex.getStatus()==Status.SERVER_ERROR_INTERNAL) {
                                    log.log(Level.SEVERE,ex.getMessage(),ex);
                                 } else {
                                    log.severe("Status="+ex.getStatus().getCode()+", "+ex.getMessage());
                                 }
                                 errorCount++;
                                 return;
                              }
                           }
                           if (src==null) {
                              // we have a regular entry
                              try {
                                 app.createEntry(user,feed,entryDoc);
                              } catch (AppException ex) {
                                 log.severe("Failed to create entry "+index.getId());
                                 if (ex.getStatus()==Status.SERVER_ERROR_INTERNAL) {
                                    log.log(Level.SEVERE,ex.getMessage(),ex);
                                 } else {
                                    log.severe("Status="+ex.getStatus().getCode()+", "+ex.getMessage());
                                 }
                                 errorCount++;
                              }
                           } else {
                              try {
                                 EntryMedia media = feed.findEntryResource(src);
                                 if (media!=null) {
                                    // We have a conflicting media entry.  We'll delete
                                    // the local one to use the pulled one
                                    Entry otherEntry = media.getEntry();
                                    final String fpath = feed.getPath();
                                    otherEntry.delete(new MediaEntryListener() {
                                       public void onDelete(EntryMedia resource) {
                                          try {
                                             storage.deleteMedia(fpath,feed.getUUID(),resource.getName());
View Full Code Here

                     // Check for media entries
                     Iterator<EntryMedia> media = entry.getResources();
                     if (media.hasNext()) {

                        // We have a media entry and so we post the media first
                        EntryMedia resource = media.next();
                        if (media.hasNext()) {
                           while (media.hasNext()) {
                              media.next();
                           }
                           errorCount++;
                           log.severe("Media entries with more than one resource is not supported.");
                           throw new SyncException("Synchronization was incomplete.  Media entry for entry "+entry.getUUID()+" has more than one resource.");
                        }

                        // Get the media from the XML DB
                        try {
                           Representation rep = storage.getMedia(path,feed.getUUID(),resource.getName());
                           // Send the media to the server
                           rep.setMediaType(resource.getMediaType());
                           appClient.createMedia(entry.getUUID(),resource.getName(),rep);
                        } catch (StatusException ex) {
                           errorCount++;
                           log.severe("Cannot update media "+resource.getName()+" on target for path "+path+", status="+ex.getStatus().getCode());
                           throw new SyncException("Synchronization was incomplete.  Failure to update media "+resource.getName()+" for entry "+entry.getUUID()+", status="+ex.getStatus().getCode());
                        } catch (Exception ex) {
                           errorCount++;
                           log.log(Level.SEVERE,"Cannot get media "+resource.getName()+" for path "+path+" and id "+entry.getUUID().toString(),ex);
                           throw new SyncException("Synchronization was incomplete.  Failure to get media "+resource.getName()+" for entry "+entry.getUUID());
                        }
                     } else {

                        String xml = null;
                        // Get the entry document from the XML DB
                        try {
                           Representation entryRep = storage.getEntry(".",path,feed.getUUID(),entry.getUUID());

                           // Now, create the feed with the XML
                           try {
                              xml = entryRep.getText();
                           } catch (IOException ex) {
                              errorCount++;
                              log.log(Level.SEVERE,"Cannot get xml serialization of entry document for path "+path,ex);
                              throw new SyncException("Synchronization was incomplete.  Failure to get serialization of entry "+entry.getUUID());
                           }
                        } catch (IOException ex) {
                           errorCount++;
                           log.log(Level.SEVERE,"Cannot get entry document for path "+path+" and id "+entry.getUUID().toString(),ex);
                           throw new SyncException("Synchronization was incomplete.  Failure to get entry "+entry.getUUID());
                        }

                        // This is a regular entry
                        try {
                           appClient.createEntry(xml);
                        } catch (Exception ex) {
                           errorCount++;
                           log.severe("Cannot create entry on target for path "+path);
                           throw new SyncException("Synchronization was incomplete.  Failure to create entry "+entry.getUUID()+" for path "+path);
                        }
                     }
                  }
               } else {
                  EntryMedia resource = updateEntry.getResource();
                  if (entry==null) {
                     // Modify feed
                     log.info("Updating feed "+feedURI);
                     // Get the feed document from the XML DB
                     try {
                        Representation feedRep = storage.getFeed(path,feed.getUUID(),feed.getEntries());

                        // Now, create the feed with the XML
                        try {
                           String xml = feedRep.getText();
                           if (!appClient.update(xml).isSuccess()) {
                              errorCount++;
                              log.severe("Cannot update feed on target for path "+path);
                              throw new SyncException("Synchronization was incomplete.  Cannot update feed for path "+path);
                           }
                        } catch (IOException ex) {
                           errorCount++;
                           log.log(Level.SEVERE,"Cannot get xml serialization of feed document for path "+path,ex);
                           throw new SyncException("Synchronization was incomplete.  Cannot get serialization of feed for path "+path);
                        }
                     } catch (IOException ex) {
                        errorCount++;
                        log.log(Level.SEVERE,"Cannot get feed document for path "+path,ex);
                        throw new SyncException("Synchronization was incomplete.  Cannot get feed for path "+path);
                     }
                  } else if (resource==null) {
                     // Modify entry
                     log.info("Updating entry "+entry.getUUID()+" for feed "+feedURI);

                     // Get the entry document from the XML DB
                     try {
                        Representation entryRep = storage.getEntry(".",path,feed.getUUID(),entry.getUUID());

                        // Now, create the feed with the XML
                        try {
                           String xml = entryRep.getText();
                           if (!appClient.updateEntry(entry.getUUID(),xml).isSuccess()) {
                              errorCount++;
                              log.severe("Cannot update entry on target for path "+path);
                              throw new SyncException("Synchronization was incomplete.  Cannot update entry "+entry.getUUID()+" for path"+path);
                           }
                        } catch (IOException ex) {
                           errorCount++;
                           log.log(Level.SEVERE,"Cannot get xml serialization of entry document for path "+path,ex);
                           throw new SyncException("Synchronization was incomplete.  Cannot serialization of entry "+entry.getUUID()+" for path"+path);
                        }
                     } catch (IOException ex) {
                        errorCount++;
                        log.log(Level.SEVERE,"Cannot get entry document for path "+path+" and id "+entry.getUUID().toString(),ex);
                        throw new SyncException("Synchronization was incomplete.  Cannot get entry "+entry.getUUID()+" for path"+path);
                     }
                  } else {
                     // Modify resource
                     log.info("Updating resource "+resource.getName()+" for entry "+entry.getUUID()+" for feed "+feedURI);

                     // Get the media from the XML DB
                     try {
                        Representation mediaRep = storage.getMedia(path,feed.getUUID(),resource.getName());
                        mediaRep.setMediaType(resource.getMediaType());
                        if (!appClient.updateMedia(resource.getName(),mediaRep).isSuccess()) {
                           errorCount++;
                           log.severe("Cannot update media "+resource.getName()+" on target for path "+path);
                           throw new SyncException("Synchronization was incomplete.  Cannot update media "+resource.getName()+" for entry "+entry.getUUID()+" for path"+path);
                        }
                     } catch (IOException ex) {
                        errorCount++;
                        log.log(Level.SEVERE,"Cannot get media "+resource.getName()+" for path "+path+" and id "+entry.getUUID().toString(),ex);
                        throw new SyncException("Synchronization was incomplete.  Cannot get media "+resource.getName()+" for entry "+entry.getUUID()+" for path"+path);
                     }
                  }
               }
            }
            proc.setLastSynchronizedOn(syncTime);
View Full Code Here

            Entry entry = entries.next();
            Document entryDoc = parser.load(app.getEntryRepresentation("", entry));
            Iterator<EntryMedia> mediaResources = entry.getResources();
            if (mediaResources.hasNext()) {
               // we have a media entry and so copy the media
               EntryMedia media = mediaResources.next();
               Representation mediaRep = app.getStorage().getMedia(source.getPath(), source.getUUID(), media.getName());
               Entry newEntry = app.createMediaEntry(user,newFeed,mediaRep,media.getName(),UUID.randomUUID());
               app.updateEntry(user,newFeed,newEntry,entryDoc);
               // TODO: copy multiple media ?
            } else {
               // Just copy the entry.  The ID will be changed since the entry exists
               app.createEntry(user, newFeed, entryDoc);
View Full Code Here

                     System.arraycopy(segments,0,path,0,segments.length-1);
                     String feedPath = App.join(path,0,path.length,'/');
                     file = segments[segments.length-1];
                     try {
                        Feed f = app.getFeed(feedPath);
                        EntryMedia media = f.findEntryResource(file);
                        if (media!=null) {
                           MediaResource r = new MediaResource(theApp,f,media,storage);
                           return r;
                        } else {
                           //getLogger().warning("No media resource "+file);
                           return null;
                        }
                     } catch (AppException nex) {
                        if (ex.getStatus().getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
                           return null;
                        } else {
                           getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+feedPath+" from the database.",nex);
                           return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
                        }

                     } catch (SQLException nex) {
                        getContext().getLogger().log(Level.SEVERE,"Failed to retrieve entry media "+file+" from feed at "+feedPath+" from the database.",nex);
                        return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
                     }
                  }
                  return null;
               } else {
                  getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+uriPath+" from the database.",ex);
                  return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
               }
            }
         }
      } else {
         String [] path = new String[segments.length-1];
         System.arraycopy(segments,0,path,0,segments.length-1);
         String feedPath = App.join(path,0,path.length,'/');
         if (file.startsWith("_entry_.")) {
            try {
               Feed f = app.getFeed(feedPath);
               if (f!=null) {
                  String entryIdS = file.substring(8);
                  int end = entryIdS.indexOf(".");
                  if (end<0) {
                     return null;
                  }
                  if (!entryIdS.substring(end).equals(".atom")) {
                     return null;
                  }
                  entryIdS = entryIdS.substring(0,end);
                  try {
                     UUID entryId = UUID.fromString(entryIdS);
                     Entry entry = f.findEntry(entryId);
                     if (entry!=null) {
                        EntryResource r = new EntryResource(theApp,f,entry,storage);
                        return r;
                     }
                  } catch (SQLException ex) {
                     getContext().getLogger().log(Level.SEVERE,"Failed to retrieve entry at "+entryIdS+" from the database.",ex);
                     return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
                  } catch (IllegalArgumentException ex) {
                     return new ErrorResource(theApp.getStatusService().getRepresentation(Status.CLIENT_ERROR_BAD_REQUEST,request,response));
                  }
               }
               return null;
            } catch (AppException ex) {
               if (ex.getStatus().getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
                  return null;
               } else {
                  getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+feedPath+" from the database.",ex);
                  return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
               }
            }
         } else if (file.charAt(0)=='.' && file.endsWith(".atom")) {
            return new ErrorResource(theApp.getStatusService().getRepresentation(Status.CLIENT_ERROR_BAD_REQUEST,request,response));
         } else {
            try {
               // The media name is already encoded
               file = URLDecoder.decode(file,"UTF-8");
               file = URLEncoder.encode(file,"UTF-8");
               Feed f = app.getFeed(feedPath);
               EntryMedia media = f.findEntryResource(file);
               if (media!=null) {
                  MediaResource r = new MediaResource(theApp,f,media,storage);
                  return r;
               } else {
                  //getLogger().warning("No media resource "+file);
View Full Code Here

TOP

Related Classes of org.atomojo.app.db.EntryMedia

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.