package api.http.google.picasa;
import api.KeyValuePair;
import api.http.HttpRequest;
import api.http.google.GoogleAPI;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class PicasaComment{
/**
* creates the metadata XML of Picasa Comment.
*
* @param comment the comment of Picasa Comment.
* @return the metadata XML of Picasa Comment.
*/
public static String createCommentXML(String comment){
StringBuilder sbr = new StringBuilder();
sbr.append("<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:media=\"http://search.yahoo.com/mrss/\" xmlns:gphoto=\"http://schemas.google.com/photos/2007\" xmlns:georss=\"http://www.georss.org/georss\" xmlns:gml=\"http://www.opengis.net/gml\">");
sbr.append(String.format("<content>%s</content>", comment));
sbr.append("<category scheme=\"http://schemas.google.com/g/2005#kind\" term=\"http://schemas.google.com/photos/2007#comment\"></category>");
sbr.append("</entry>");
return sbr.toString();
}
private PicasaPhoto picasaPhoto;
private String commentId;
private String comment;
private Date lastModified;
private URL editUrl;
/**
* returns the Picasa Photo object which cantains this object.
*
* @return the Picasa Photo object which cantains this object.
*/
public PicasaPhoto getPicasaPhoto(){
return picasaPhoto;
}
/**
* returns the comment id.
*
* @return the comment id.
*/
public String getCommentId(){
return commentId;
}
/**
* returns the comment id.
*
* @return the comment id.
*/
public String getComment(){
return comment;
}
/**
* returns the last modified.
*
* @return the last modified.
*/
public Date getLastModified(){
return lastModified;
}
/**
* returns the edit url.
*
* @return the edit url.
*/
public URL getEditUrl(){
return editUrl;
}
PicasaComment(PicasaPhoto picasaPhoto, Element documentRoot){
this.picasaPhoto = picasaPhoto;
this.initial(documentRoot);
}
/**
* constructs this object with given user and album id.
*
* @param user Google user.
* @param albumId Picasa Album id.
* @param photoId Picasa Photo id.
* @param commentId Picasa Comment id.
* @param googleapi the Google API with exist auth token.
* @throws IOException the url is not exist or something wrong of the server
* of target url.
* @throws ParserConfigurationException the response is not a parsable XML.
* @throws SAXException the response is not a parsable XML.
*/
public PicasaComment(String user, String albumId, String photoId, String commentId, GoogleAPI googleapi) throws IOException, ParserConfigurationException, SAXException{
PicasaWeb $picasaWeb = new PicasaWeb(user, googleapi);
PicasaAlbum $picasaAlbum = $picasaWeb.getAlbum(albumId);
PicasaPhoto $picasaPhoto = $picasaAlbum.getPhoto(photoId);
PicasaComment $picasaComment = $picasaPhoto.getComment(commentId);
this.picasaPhoto = $picasaComment.picasaPhoto;
this.comment = $picasaComment.comment;
this.commentId = $picasaComment.commentId;
this.lastModified = $picasaComment.lastModified;
this.editUrl = $picasaComment.editUrl;
}
/**
* constructs this object with given user and album id.
*
* @param user Google user.
* @param albumId Picasa Album id.
* @param photoId Picasa Photo id.
* @param commentId Picasa Comment id.
* @throws IOException the url is not exist or something wrong of the server
* of target url.
* @throws ParserConfigurationException the response is not a parsable XML.
* @throws SAXException the response is not a parsable XML.
*/
public PicasaComment(String user, String albumId, String photoId, String commentId) throws IOException, ParserConfigurationException, SAXException{
this(user, albumId, photoId, commentId, null);
}
private void initial(Element documentRoot){
try{
this.comment = documentRoot.getElementsByTagName("content").item(0).getTextContent();
} catch (Exception ex){
}
try{
this.commentId = documentRoot.getElementsByTagName("gphoto:id").item(0).getTextContent();
} catch (Exception ex){
}
try{
this.lastModified = new Date(Long.parseLong(documentRoot.getElementsByTagName("gphoto:timestamp").item(0).getTextContent()));
} catch (Exception ex){
}
try{
NodeList links = documentRoot.getElementsByTagName("link");
for (int i = 0; i < links.getLength(); i++){
Node link = links.item(i);
if (link.getParentNode() == documentRoot){
try{
NamedNodeMap attributes = link.getAttributes();
String rel = attributes.getNamedItem("rel").getTextContent();
URL href = new URL(attributes.getNamedItem("href").getTextContent());
if (rel.equals("edit")){
this.editUrl = href;
}
} catch (Exception ex){
}
}
}
} catch (Exception ex){
}
}
public void delete() throws IOException{
HttpRequest httpRequest = new HttpRequest(this.editUrl);
if (this.picasaPhoto.getPicasaAlbum().getPicasaWeb().getGoogleapi() != null){
KeyValuePair pair = this.picasaPhoto.getPicasaAlbum().getPicasaWeb().getGoogleapi().toAuthHeader();
httpRequest.addHeader(pair.key, pair.value);
}
httpRequest.setMethod(HttpRequest.METHOD_DELETE);
httpRequest.setContentType("application/atom+xml");
String response = httpRequest.send();
this.comment = null;
this.commentId = null;
this.editUrl = null;
this.lastModified = null;
}
@Override
public String toString(){
StringBuilder sbr = new StringBuilder();
sbr.append(String.format("comment = \"%s\"\n", this.comment));
sbr.append(String.format("comment id = \"%s\"\n", this.commentId));
sbr.append(String.format("last modified = \"%s\"\n", ((this.lastModified == null) ? System.currentTimeMillis() : this.lastModified.getTime())));
sbr.append(String.format("edit url = \"%s\"\n", ((this.editUrl == null) ? "" : this.editUrl.toString())));
return sbr.toString();
}
}