1 package yarfraw.mapping.forward.impl;
2
3 import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toAtomId;
4 import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toCategoryType;
5 import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toEntry;
6 import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toIcon;
7 import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toLink;
8 import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toPersonType;
9 import static yarfraw.mapping.forward.impl.Atom10MappingUtils.toTextType;
10
11 import java.util.List;
12
13 import javax.xml.bind.JAXBElement;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import yarfraw.core.datamodel.CategorySubject;
19 import yarfraw.core.datamodel.ChannelFeed;
20 import yarfraw.core.datamodel.FeedFormat;
21 import yarfraw.core.datamodel.Generator;
22 import yarfraw.core.datamodel.ItemEntry;
23 import yarfraw.core.datamodel.Link;
24 import yarfraw.core.datamodel.Person;
25 import yarfraw.core.datamodel.YarfrawException;
26 import yarfraw.generated.atom10.elements.DateTimeType;
27 import yarfraw.generated.atom10.elements.FeedType;
28 import yarfraw.generated.atom10.elements.GeneratorType;
29 import yarfraw.generated.atom10.elements.ObjectFactory;
30 import yarfraw.mapping.forward.ToAtom10Channel;
31 import yarfraw.utils.CommonUtils;
32 /***
33 * Util methods for mapping Yarfraw core model to Atom10 Jaxb model
34 * @author jliang
35 *
36 */
37 public class ToAtom10ChannelImpl implements ToAtom10Channel{
38 private static final Log LOG = LogFactory.getLog(ToAtom10ChannelImpl.class);
39 private static ToAtom10Channel _instance = new ToAtom10ChannelImpl();
40 private static final ObjectFactory FACTORY = new ObjectFactory();
41
42 public static ToAtom10Channel getInstance(){
43 return _instance;
44 }
45 private ToAtom10ChannelImpl(){}
46
47 /***
48 * atomEntry =
49 element atom:entry {
50 atomCommonAttributes,
51 (atomAuthor*
52 & atomCategory*
53 & atomContent?
54 & atomContributor*
55 & atomId
56 & atomLink*
57 & atomPublished?
58 & atomRights?
59 & atomSource?
60 & atomSummary?
61 & atomTitle
62 & atomUpdated
63 & extensionElement*)
64 }
65 */
66 public JAXBElement<FeedType> execute(ChannelFeed ch) throws YarfrawException {
67 ObjectFactory factory = FACTORY;
68 FeedType ret = factory.createFeedType();
69 List<Object> elementList = ret.getAuthorOrCategoryOrContributor();
70
71 if(ch.getUid() != null){
72 elementList.add(factory.createEntryTypeId(toAtomId(ch.getUid())));
73 }
74
75 if(ch.getManagingEditorOrAuthorOrPublisher() != null){
76 for(Person author : ch.getManagingEditorOrAuthorOrPublisher()){
77 elementList.add(factory.createFeedTypeAuthor(toPersonType(author)));
78 }
79 }
80
81
82 if(ch.getCategorySubjects() != null){
83 for(CategorySubject c : ch.getCategorySubjects()){
84 if(c != null){
85 elementList.add(factory.createFeedTypeCategory(toCategoryType(c)));
86 }
87 }
88 }
89
90 if(ch.getContributors() != null){
91 for(Person c : ch.getContributors()){
92 elementList.add(factory.createFeedTypeContributor(toPersonType(c)));
93 }
94 }
95
96 if(ch.getGenerator() != null){
97 GeneratorType g = factory.createGeneratorType();
98 Generator in = ch.getGenerator();
99 g.setUri(in.getUri());
100 g.setValue(in.getValue());
101 g.setVersion(in.getVersion());
102
103 if(in.getOtherAttributes() != null){
104 g.getOtherAttributes().putAll(in.getOtherAttributes());
105 }
106
107 g.setBase(in.getBase());
108 g.setLang(in.getLang());
109 elementList.add(factory.createFeedTypeGenerator(g));
110 }
111
112 if(ch.getImageOrIcon() != null){
113 elementList.add(factory.createFeedTypeIcon(toIcon(ch.getImageOrIcon())));
114 }
115
116 if(ch.getUid() != null){
117 elementList.add(factory.createFeedTypeId(toAtomId(ch.getUid())));
118 }
119
120 if(ch.getLinks() != null ){
121 for(Link link : ch.getLinks()){
122 elementList.add(factory.createFeedTypeLink(toLink(link)));
123 }
124 }
125
126 if(ch.getLogo() != null){
127 elementList.add(factory.createFeedTypeIcon(toIcon(ch.getLogo())));
128 }
129
130 if(ch.getRights() != null){
131 elementList.add(factory.createFeedTypeRights(
132 toTextType(ch.getRights())));
133 }
134
135 if(ch.getDescriptionOrSubtitle() != null){
136 elementList.add(factory.createFeedTypeSubtitle(
137 toTextType(ch.getDescriptionOrSubtitle())));
138 }
139
140 if(ch.getTitle() != null){
141 elementList.add(factory.createFeedTypeTitle(
142 toTextType(ch.getTitle())));
143 }
144
145 if(ch.getLastBuildOrUpdatedDate() != null){
146 DateTimeType date = factory.createDateTimeType();
147 String dateString = ch.getLastBuildOrUpdatedDate();
148 if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.ATOM10)){
149 String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.ATOM10);
150 if(newDateString != null){
151 dateString = newDateString;
152 }else{
153 LOG.warn("The dateString "+dateString+" is in valid according to ATOM 1.0 specs, unabel to convert it to a valid format, writing it as is");
154 }
155 }
156 date.setValue(dateString);
157 elementList.add(factory.createFeedTypeUpdated(date));
158 }
159
160
161 if(ch.getPubDate() != null){
162 LOG.info("PubDate under <feed> level is not supported, it will be ignored");
163 }
164
165 if(ch.getItems() != null){
166 for(ItemEntry item : ch.getItems()){
167 elementList.add(factory.createFeedTypeEntry(toEntry(item)));
168 }
169 }
170
171 ret.setBase(ch.getBase());
172 ret.setLang(ch.getLang());
173
174 if(ch.getOtherElements() != null){
175 elementList.addAll(ch.getOtherElements());
176 }
177 if(ch.getOtherAttributes() != null){
178 ret.getOtherAttributes().putAll(ch.getOtherAttributes());
179 }
180 return factory.createFeed(ret);
181 }
182
183 }