Results 1 to 4 of 4
  1. #1
    missy786 is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Understanding xml content class.

    Hello,

    I am writing to seek help, in understanding, what the code below is trying to execute and its logic. This is code is under class called content.java. Any help would be very much appreciated. Many thanks.

    Java Code:
    	public static ApiObject createArticle(
    			NotessaApi api, ApiObject issue, 
    			Map<String, String> sectionNameToXml, Article article) throws IOException {
    		Document document = Utils.createEmptyDocument();
    		Node node = Utils.createNode(document, "article");
    		
    		Utils.addXml(document, node, "title", article.getTitle());
    		Utils.addXml(document, node, "subtitle", article.getSubtitle());
    		Utils.addXml(document, node, "author", article.getAuthor());
    		Utils.addXml(document, node, "synopsis", article.getSynopsis());
    		Utils.addXml(document, node, "body", article.getBody());
    		Utils.addXml(document, node, "status", article.getStatus());
    		Utils.addXml(document, node, "url", article.getUrl());
    		Utils.addXml(document, node, "display_date", article.getDisplayDate());
    		
    		ApiObject created = api.create(issue.findLink("article"), Utils.renderXml(document));
    		
    		ApiObject articleObject = api.resolveCreated(created);
    		
    		attachImage(api, articleObject, "body_image", article.getBodyImage());
    		attachImage(api, articleObject, "synopsis_image", article.getSynopsisImage());
    		
    		for(String section : article.getSections()) {
    			String sectionXml = sectionNameToXml.get(section);
    			if(sectionXml == null) {
    				throw new RuntimeException("Unknown section: " + section);
    			}
    			api.create(articleObject.findLink("section"), sectionXml);
    		}
    		
    		return created;
    	}
    	
    	private static void attachImage(NotessaApi api, ApiObject created, String imageLocation, Image image) throws IOException {
    		if(image == null) {
    			return;
    		}
    		URL link = created.findLink(imageLocation);
    		api.sendImage(link, image);
    	}
    
    	public static ApiObject createIssue(NotessaApi api, ApiObject publication, Issue issue) throws IOException {
    		Document document = Utils.createEmptyDocument();
    		Node node = Utils.createNode(document, "issue");
    		
    		Utils.addXml(document, node, "title", issue.getTitle());
    		Utils.addXml(document, node, "template", issue.getTemplate());
    		Utils.addXml(document, node, "description", issue.getDescription());
    		Utils.addXml(document, node, "approval_status", issue.getApprovalStatus());
    		Utils.addXml(document, node, "display_title", issue.getDisplayTitle());
    		Utils.addXml(document, node, "display_date", issue.getDisplayDate());
    		Utils.addXml(document, node, "issue_number", issue.getIssueNumber());
    		List<Property> properties = issue.getProperties();
    		if(!properties.isEmpty()) {
    			Node container = Utils.createNode(document, node, "properties");
    			for(Property property : properties) {
    				Element element = Utils.createNode(document, container, "property");
    				element.setAttribute("name", property.getName());
    				Utils.addXml(document, element, "value", property.getValue());
    			}
    		}
    		
    		return api.create(publication.findLink("issue"), Utils.renderXml(document));
    	}
    
    	public static String determineTemplate(NotessaApi api, ApiObject publication, String templateTitle) throws IOException {
    		URL issues = publication.findLink("issue");
    		ApiObject issueList = api.get(issues, "fields=title,template");
    	    return issueList.xpath("/issues/issue[title='" + templateTitle + "']/template");
    	}
    
    	public static ApiObject getPublicationByCode(NotessaApi api, String publicationCode) throws IOException {
    		return api.get("publication/" + publicationCode);
    	}
    	
    	public static ApiObject buildContent(Properties properties, Collection<Article> articles) throws IOException {
    		NotessaApi api = NotessaApi.buildApi(properties, "content");
    		
    		String publicationCode = properties.getProperty("content.publicationCode");
    		System.out.println("Retrieving publication");
    		ApiObject publication = ContentCreator.getPublicationByCode(api, publicationCode);
    		
    		String templateIssueTitle = properties.getProperty("content.templateIssueTitle");
    		System.out.println("Determining template");
    		String template = ContentCreator.determineTemplate(api, publication, templateIssueTitle);
    		
    		System.out.println("Preparing section list");
    		ApiObject sectionList = api.get(publication.findLink("section"));
    		Map<String, String> sectionNameToXml = buildSectionMap(sectionList);
    		
    		ApprovalStatus approvalStatus = ApprovalStatus.valueOf(properties.getProperty("content.approvalStatus"));
    		
    		Issue issue = new Issue();
    		issue.setTitle(properties.getProperty("content.issueTitle"));
    		issue.setTemplate(template);
    		issue.setApprovalStatus(approvalStatus);
    		
    		System.out.println("Creating issue");
    		ApiObject issueStatus = ContentCreator.createIssue(api, publication, issue);
    		ApiObject createdIssue = api.resolveCreated(issueStatus);	
    		
    		System.out.println("Creating " + articles.size() + " articles...");
    		for(Article article : articles) {
    			if(article.getStatus() == null) {
    				article.setStatus(approvalStatus);
    			}
    			ContentCreator.createArticle(api, createdIssue, sectionNameToXml, article);
    		}
    		return createdIssue;
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Understanding xml content class.

    Can you add the import statements that show what packages the classes used in the code come from?
    There should be some API doc for those classes that will help you understand what they are doing.
    The classes are not part of the Java SE.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    missy786 is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Re: Understanding xml content class.

    Thank you for your response back. The following are the imports, that came with the class.
    I am having hard time locating the API docs, hence I am seeking help, to figure the purpose of this class, especially the "buildContent" method.

    Java Code:
    import java.io.IOException;
    import java.net.URL;
    import java.util.Collection;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    import com.newsW.api.domain.content.ApprovalStatus;
    import com.newsW.api.domain.content.Article;
    import com.newsW.api.domain.content.Image;
    import com.newsW.api.domain.content.Issue;
    import com.newsW.api.domain.content.Property;
    import com.newsW.api.util.Utils;
    Many thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Understanding xml content class.

    I am having hard time locating the API docs,
    Have you tried an internet search?
    As I said, many of those packages and classes are NOT part of Java SE. You need to ask the people that wrote those packages what they do and how to use them.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. understanding class
    By mitra in forum New To Java
    Replies: 10
    Last Post: 07-26-2011, 03:24 PM
  2. Problem understanding class statement.
    By martijnburger in forum New To Java
    Replies: 3
    Last Post: 07-22-2011, 12:07 AM
  3. Understanding -- new Class() -- syntax.
    By AcousticBruce in forum New To Java
    Replies: 8
    Last Post: 12-14-2010, 01:53 AM
  4. Having trouble understanding Class Graphic
    By Bernard Robitaille in forum JCreator
    Replies: 1
    Last Post: 04-18-2009, 03:55 AM
  5. Replies: 3
    Last Post: 10-07-2008, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •