Coverage Report - yarfraw.mapping.forward.impl.Rss10MappingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
Rss10MappingUtils
83% 
89% 
0
 
 1  
 package yarfraw.mapping.forward.impl;
 2  
 
 3  
 import static yarfraw.utils.CommonConstants.MIN_PER_DAY;
 4  
 import static yarfraw.utils.CommonConstants.MIN_PER_MONTH;
 5  
 import static yarfraw.utils.CommonConstants.MIN_PER_WEEK;
 6  
 import static yarfraw.utils.CommonConstants.MIN_PER_YEAR;
 7  
 
 8  
 import java.math.BigInteger;
 9  
 import java.util.List;
 10  
 
 11  
 import javax.xml.bind.JAXBElement;
 12  
 
 13  
 import org.apache.commons.collections.CollectionUtils;
 14  
 import org.apache.commons.logging.Log;
 15  
 import org.apache.commons.logging.LogFactory;
 16  
 
 17  
 import yarfraw.core.datamodel.CategorySubject;
 18  
 import yarfraw.core.datamodel.ChannelFeed;
 19  
 import yarfraw.core.datamodel.FeedFormat;
 20  
 import yarfraw.core.datamodel.Image;
 21  
 import yarfraw.core.datamodel.ItemEntry;
 22  
 import yarfraw.core.datamodel.TextInput;
 23  
 import yarfraw.generated.rss10.elements.DcType;
 24  
 import yarfraw.generated.rss10.elements.Items;
 25  
 import yarfraw.generated.rss10.elements.Li;
 26  
 import yarfraw.generated.rss10.elements.ObjectFactory;
 27  
 import yarfraw.generated.rss10.elements.Seq;
 28  
 import yarfraw.generated.rss10.elements.TRss10Channel;
 29  
 import yarfraw.generated.rss10.elements.TRss10Image;
 30  
 import yarfraw.generated.rss10.elements.TRss10TextInput;
 31  
 import yarfraw.generated.rss10.elements.UpdatePeriodEnum;
 32  
 import yarfraw.utils.CommonUtils;
 33  
 
 34  
 /**
 35  
  * Util methods for mapping Yarfraw core model to Rss10 Jaxb model
 36  
  * @author jliang
 37  
  *
 38  
  */
 39  12
 class Rss10MappingUtils {
 40  0
   private Rss10MappingUtils(){}
 41  12
   private static final ObjectFactory FACTORY = new ObjectFactory ();
 42  12
   private static final Log LOG = LogFactory.getLog(Rss10MappingUtils.class);
 43  
   public static JAXBElement<TRss10Image> toRss10Image(Image in){
 44  12
     TRss10Image ret = FACTORY.createTRss10Image();
 45  
     //not supported
 46  12
     if(in.getDescription() != null
 47  12
         || in.getHeight() != null
 48  0
         || in.getWidth() != null){
 49  12
       LOG.info("description, height, width are not supported in Rss 1.0's image element. They will be ignored");
 50  
     }
 51  
     
 52  12
     ret.setTitle(in.getTitle());
 53  12
     ret.setUrl(in.getUrl());
 54  12
     ret.setLink(in.getLink());
 55  
     
 56  12
     ret.setAbout(in.getAbout());
 57  12
     ret.setResource(in.getResource());
 58  
     
 59  12
     if(ret.getAbout() == null){
 60  3
       ret.setAbout(in.getUrl());
 61  
     }
 62  
     
 63  12
     return FACTORY.createTRss10ChannelImage(ret);
 64  
   }
 65  
 
 66  
   public static JAXBElement<TRss10TextInput> toRss10TextInput(TextInput in) {
 67  9
     TRss10TextInput ret = FACTORY.createTRss10TextInput();
 68  9
     List<Object> elementList = ret.getTitleOrDescriptionOrName();
 69  
     
 70  9
     if(in.getTitle() != null){
 71  9
       elementList.add(FACTORY.createTRss10TextInputTitle(in.getTitle()));
 72  
     }
 73  
     
 74  9
     if(in.getDescription() != null){
 75  9
       elementList.add(FACTORY.createTRss10TextInputDescription(in.getDescription()));
 76  
     }
 77  9
     if(in.getName() != null){
 78  9
       elementList.add(FACTORY.createTRss10TextInputName(in.getName()));
 79  
     }
 80  9
     if(in.getLink() != null){
 81  9
       elementList.add(FACTORY.createTRss10TextInputLink(in.getLink()));
 82  
     }
 83  
     
 84  9
     if(in.getOtherElements() != null){
 85  9
       elementList.addAll(in.getOtherElements());
 86  
     }
 87  9
     ret.setAbout(in.getAbout());
 88  9
     ret.setResource(in.getResource());
 89  
     
 90  9
     if(ret.getAbout() == null){
 91  0
       ret.setAbout(in.getLink());
 92  
     }
 93  9
     return FACTORY.createTextinput(ret);
 94  
   }
 95  
   /*
 96  
    *  (title, link, description, image?, items, textinput?)
 97  
    */
 98  
   public static JAXBElement<TRss10Channel> toChannel(ChannelFeed ch){
 99  15
     ObjectFactory factory = FACTORY;
 100  15
     TRss10Channel ret = factory.createTRss10Channel();
 101  15
     List<Object> elementList = ret.getTitleOrLinkOrDescription();
 102  
     
 103  15
     if(ch.getTitleText() != null){
 104  15
       elementList.add(factory.createTRss10ChannelTitle(ch.getTitleText()));
 105  
     }
 106  
     
 107  15
     String link = Utils.getHrefLink(ch.getLinks());
 108  15
     if(link != null){
 109  15
       elementList.add(factory.createTRss10ChannelLink(link));
 110  
     }
 111  
     
 112  15
     if(ch.getDescriptionOrSubtitleText() != null){
 113  15
       elementList.add(factory.createTRss10ChannelDescription(ch.getDescriptionOrSubtitleText()));
 114  
     }
 115  
     
 116  15
     if(ch.getImageOrIcon() != null){
 117  12
       TRss10Image img = factory.createTRss10Image();
 118  12
       img.setResource(ch.getImageOrIcon().getAbout());
 119  12
       if(img.getResource() == null){
 120  3
         img.setAbout(img.getUrl());
 121  
       }
 122  12
       elementList.add(factory.createTRss10ChannelImage(img));
 123  
     }
 124  
     
 125  15
     if(ch.getTexInput() != null){
 126  9
       TRss10TextInput ti = factory.createTRss10TextInput();
 127  9
       ti.setResource(ch.getTexInput().getAbout());
 128  9
       if(ti.getResource() == null){
 129  0
         ti.setResource(ch.getTexInput().getLink());
 130  
       }
 131  9
       elementList.add(factory.createTextinput(ti));
 132  
     }
 133  
     
 134  15
     if(ch.getCategorySubjects() != null){
 135  0
       for(CategorySubject c : ch.getCategorySubjects()){
 136  0
         if(c != null){
 137  0
           DcType dc = new DcType();
 138  0
           dc.setValue(c.getCategoryOrSubjectOrTerm());
 139  0
           elementList.add(factory.createSubject(dc));
 140  
         }
 141  
       }
 142  
     }
 143  
     
 144  
     //NOT SUPPORTED
 145  15
     if(ch.getCloud() != null){
 146  0
       LOG.info("Channel.Cloud is not supported in Rss 1.0 feed. It will be ignored.");
 147  
     }
 148  
     
 149  15
     if(ch.getRightsText() != null){
 150  12
       DcType dc = new DcType();
 151  12
       dc.setValue(ch.getRightsText());
 152  12
       elementList.add(factory.createRights(dc));
 153  
     }
 154  
     
 155  
   //NOT SUPPORTED
 156  15
     if(ch.getDocs() != null){
 157  3
       LOG.info("Channel.Docs is not supported in Rss 1.0 feed. It will be ignored.");
 158  
     }
 159  
     //NOT SUPPORTED
 160  15
     if(ch.getGenerator() != null){
 161  6
       LOG.info("Channel.Generator is not supported in Rss 1.0 feed. It will be ignored.");
 162  
     }
 163  
 
 164  
 
 165  15
     Seq seq = factory.createSeq();
 166  15
     if(ch.getItems() != null){
 167  84
       for(ItemEntry t : ch.getItems()){
 168  54
         if(t != null){
 169  54
           Li li = factory.createLi();
 170  54
           if(t.getResource() != null){
 171  0
             li.setResource(t.getResource());
 172  54
           }else if(CollectionUtils.isNotEmpty(t.getLinks())){
 173  51
             li.setResource(t.getLinks().get(0).getHref()); //use the link if no resource was specified
 174  
           }else{
 175  3
             LOG.warn("no <link> found under Item, unable to create <li> element under <items>");
 176  
           }
 177  54
           seq.getLi().add(li);
 178  
         }
 179  
       }
 180  
     }
 181  
     
 182  15
     Items items =factory.createItems();
 183  15
     items.setSeq(seq);
 184  15
     elementList.add(factory.createItems(items));
 185  
     
 186  15
     if(ch.getLang() != null){
 187  6
       DcType dc = new DcType();
 188  6
       dc.setValue(ch.getLang());
 189  6
       elementList.add(factory.createLanguage(dc));
 190  
     }
 191  
 
 192  
     //not supported
 193  15
     if(ch.getLastBuildOrUpdatedDate() != null){
 194  6
       LOG.info("Channel.LastBuildDate is not supported in Rss 1.0 feed. It will be ignored.");
 195  
     }
 196  
     
 197  15
     String creator = Utils.getEmailOrText(ch.getWebMasterOrCreator());
 198  15
     if(creator != null){
 199  15
       DcType dc = new DcType();
 200  15
       dc.setValue(creator);
 201  15
       elementList.add(factory.createCreator(dc));
 202  
     }
 203  
     
 204  15
     if(ch.getPubDate() != null){
 205  15
       String dateString = ch.getPubDate();
 206  15
       if(!CommonUtils.isDateFormatValid(dateString, FeedFormat.RSS10)){
 207  6
         String newDateString = CommonUtils.formatDate(CommonUtils.tryParseDate(dateString), FeedFormat.RSS10);
 208  6
         if(newDateString != null){
 209  6
           dateString = newDateString;
 210  
         }else{
 211  0
           LOG.warn("The dateString "+dateString+" is in valid according to RSS 1.0 specs, unabel to convert it to a valid format, writing it as is");
 212  
         }
 213  
       }
 214  15
       DcType dc = new DcType();
 215  15
       dc.setValue(dateString);
 216  15
       elementList.add(factory.createDate(dc));
 217  
     }
 218  
     
 219  
 //  not supported
 220  15
     if(ch.getSkipDays() != null){
 221  0
       LOG.info("Channel.SkipDays is not supported in Rss 1.0 feed. It will be ignored.");
 222  
     }
 223  
 
 224  15
     if(ch.getSkipHours() != null){
 225  0
       LOG.info("Channel.SkipHours is not supported in Rss 1.0 feed. It will be ignored.");
 226  
     }
 227  
 
 228  
 
 229  15
     if(ch.getTtl() != null){
 230  12
       int ttl = ch.getTtl().intValue();
 231  12
       UpdatePeriodEnum updatedPeriod = UpdatePeriodEnum.DAILY;
 232  12
       int frequency = 1;
 233  
       //calculate updatePeriod, updateFrequency using ttl
 234  12
       if(ttl < 60){
 235  9
         updatedPeriod = UpdatePeriodEnum.HOURLY;
 236  9
         frequency = Math.max(1, 60/ttl);
 237  3
       }else if(ttl < MIN_PER_DAY){
 238  3
         frequency = Math.max(1, MIN_PER_DAY/ttl);
 239  0
       }else if(ttl < MIN_PER_WEEK){
 240  0
         updatedPeriod = UpdatePeriodEnum.WEEKLY;
 241  0
         frequency = Math.max(1, MIN_PER_WEEK/ttl);
 242  0
       }else if(ttl < MIN_PER_MONTH){
 243  0
         updatedPeriod = UpdatePeriodEnum.MONTHLY;
 244  0
         frequency = Math.max(1, MIN_PER_MONTH/ttl);
 245  
       }else{
 246  0
         updatedPeriod = UpdatePeriodEnum.YEARLY;
 247  0
         frequency = Math.max(1, MIN_PER_YEAR/ttl);
 248  
       }
 249  12
       elementList.add(factory.createUpdatePeriod(updatedPeriod));
 250  12
       elementList.add(factory.createUpdateFrequency(new BigInteger(String.valueOf(frequency))));
 251  
     }
 252  
 
 253  15
     String publisher = Utils.getEmailOrText(ch.getManagingEditorOrAuthorOrPublisher());
 254  15
     if(publisher != null){
 255  15
       DcType dc = new DcType();
 256  15
       dc.setValue(publisher);
 257  15
       elementList.add(factory.createPublisher(dc));
 258  
     } 
 259  
     
 260  15
     String contributor = Utils.getEmailOrText(ch.getContributors());
 261  15
     if(contributor != null){
 262  0
       DcType dc = new DcType();
 263  0
       dc.setValue(contributor);
 264  0
       elementList.add(factory.createContributor(dc));
 265  
     }
 266  
     
 267  15
     ret.setAbout(ch.getAbout());
 268  15
     ret.setResource(ch.getResource());
 269  
     
 270  15
     if(ret.getAbout() == null){
 271  6
       ret.setAbout(Utils.getHrefLink(ch.getLinks()));
 272  
     }
 273  
     
 274  15
     if(ch.getOtherElements() != null){
 275  15
       elementList.addAll(ch.getOtherElements());
 276  
     }
 277  15
     if(ch.getOtherAttributes() != null){
 278  15
       ret.getOtherAttributes().putAll(ch.getOtherAttributes());
 279  
     }
 280  15
     return factory.createChannel(ret);
 281  
   }
 282  
 }