package com.ibm.test.wcm.app.atom.resolver; import java.util.Date; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import org.apache.abdera.factory.Factory; import org.apache.abdera.model.Entry; import org.apache.abdera.model.Feed; import org.apache.abdera.model.Person; import org.apache.abdera.parser.stax.FOMFactory; import org.apache.abdera.util.iri.IRISyntaxException; import com.ibm.workplace.wcm.api.AuthoringTemplate; import com.ibm.workplace.wcm.api.Content; import com.ibm.workplace.wcm.api.DocumentId; import com.ibm.workplace.wcm.api.DocumentIdIterator; import com.ibm.workplace.wcm.api.DocumentTypes; import com.ibm.workplace.wcm.api.OptionSelectionComponent; import com.ibm.workplace.wcm.api.Workspace; import com.ibm.workplace.wcm.api.exceptions.AuthorizationException; import com.ibm.workplace.wcm.api.exceptions.DocumentRetrievalException; import com.ibm.test.wcm.app.atom.util.AtomURLBuilder; public class NewFeedWCMResolver { public NewFeedWCMResolver() { // TODO Auto-generated constructor stub } public Feed getFeed(Workspace p_workspace, AtomURLBuilder p_urlBuilder, Locale locale, String p_atName, String p_optionAttr, String p_optionVal, HttpServletRequest p_request) throws AuthorizationException, Exception { p_workspace.setCurrentDocumentLibrary(p_workspace.getDocumentLibrary("shared resources")); DocumentIdIterator ATDocumentIdIterator = p_workspace.findByName(DocumentTypes.AuthoringTemplate, p_atName); DocumentId templateDocumentId = ATDocumentIdIterator.nextId(); Feed feed = s_abderaFactory.newFeed(); addFeedHeaders(p_workspace, p_urlBuilder, feed, templateDocumentId, locale, p_request); addFeedEntries(p_workspace, p_urlBuilder, feed, templateDocumentId, locale, p_optionAttr, p_optionVal, p_request); return feed; } protected void addFeedHeaders(Workspace p_workspace, AtomURLBuilder p_urlBuilder, Feed p_feed, DocumentId p_templateDocumentId, Locale p_locale, HttpServletRequest p_request) throws AuthorizationException, Exception { try { p_feed.setGenerator(p_urlBuilder.urlForGenerator(), "1.0", "Teamspace Web Content"); } catch(IRISyntaxException e) { throw new Exception("Internationalized Resource syntax exception while creating feed header", e); } AuthoringTemplate authoringTemplate = null; try { authoringTemplate = (AuthoringTemplate)p_workspace.getById(p_templateDocumentId); } catch (DocumentRetrievalException e) { throw new Exception("Document retrival exception while creating feed header", e); } catch (AuthorizationException e) { throw new Exception("Authorization exception while creating feed header", e); } String [] authors = authoringTemplate.getAuthors(); if(authors != null && authors.length > 0) { String authorDN = authors[0]; p_feed.addAuthor(getAuthor(p_urlBuilder, authorDN, p_request)); } String filteredTitle = authoringTemplate.getTitle(); p_feed.setTitle(filteredTitle); p_feed.setUpdated(new Date()); try { p_feed.setId(authoringTemplate.getId().toString()); } catch(IRISyntaxException e) { throw new Exception("Internationalized Resource syntax exception while creating feed", e); } } protected void addFeedEntries(Workspace p_workspace, AtomURLBuilder p_urlBuilder, Feed p_feed, DocumentId p_templateDocumentId, Locale p_locale, String p_optionAttr, String p_optionVal, HttpServletRequest p_request) throws Exception { try { DocumentIdIterator feedControllablesIterator = null; feedControllablesIterator = p_workspace.contentSearch(p_templateDocumentId, null,null,null); while(feedControllablesIterator.hasNext()) { DocumentId tempControllable = (DocumentId)feedControllablesIterator.next(); Content OptionSelection = (Content)p_workspace.getById(tempControllable) ; OptionSelectionComponent prioritySelection = (OptionSelectionComponent)OptionSelection.getComponent(p_optionAttr); if (prioritySelection != null ) { String[] selections = prioritySelection.getSelections(); if ( selections != null && selections.length > 0 ) { // Should only be a single slection, but well check them all for ( int len=0; len 0) { String authorUID = authors[0]; author = getAuthor(p_urlBuilder, authorUID, p_request); entry.addAuthor(author); } String title = p_content.getTitle(); String resultFieldsTitle; try { if(title.startsWith("$COMMENT$ ")) { resultFieldsTitle = author.getName() ; } else { resultFieldsTitle = title; } entry.setTitle(resultFieldsTitle); entry.setUpdated(p_content.getModifiedDate()); String filteredDescription = p_content.getDescription(); entry.setSummary(filteredDescription.toString()); } catch (Exception e) { throw new Exception("", e); } String id = getId(p_content.getId().toString()); try { entry.setId(p_urlBuilder.urnForId(id), false); entry.addLink(p_urlBuilder.urlForId(id)); } catch(IRISyntaxException e) { throw new Exception("Internationalized Resource syntax exception while creating feed entry", e); } return entry; } protected Person getAuthor(AtomURLBuilder p_urlBuilder, String p_uid, HttpServletRequest p_request) throws Exception { Person person = s_abderaFactory.newAuthor(); try { person.setUri(p_uid); } catch(IRISyntaxException e) { throw new Exception("Internationalized Resource syntax exception while fetching user details for feed entry", e); } return person; } public String getId(String str) { return str.substring(str.indexOf("/")+1,str.indexOf("/",str.indexOf("/")+1)); } protected static final Factory s_abderaFactory = new FOMFactory(); }