Author: tchemit Date: 2008-07-02 09:28:07 +0000 (Wed, 02 Jul 2008) New Revision: 93 Modified: trunk/lutinrss/pom.xml trunk/lutinrss/src/main/java/org/codelutin/rss/RSSGenerator.java trunk/lutinrss/src/main/java/org/codelutin/rss/RSSIOUtil.java Log: amelioration de l'update d'un feed (possibilite de recuperer les champs qui ont ete modifies) Modified: trunk/lutinrss/pom.xml =================================================================== --- trunk/lutinrss/pom.xml 2008-07-01 12:46:09 UTC (rev 92) +++ trunk/lutinrss/pom.xml 2008-07-02 09:28:07 UTC (rev 93) @@ -24,7 +24,7 @@ <packaging>war</packaging> <!--Version--> - <version>2.3.6</version> + <version>2.3.7</version> <!--Description--> <description>Servlet pouvant etre appeler depuis du JS pour recuperer un Modified: trunk/lutinrss/src/main/java/org/codelutin/rss/RSSGenerator.java =================================================================== --- trunk/lutinrss/src/main/java/org/codelutin/rss/RSSGenerator.java 2008-07-01 12:46:09 UTC (rev 92) +++ trunk/lutinrss/src/main/java/org/codelutin/rss/RSSGenerator.java 2008-07-02 09:28:07 UTC (rev 93) @@ -19,6 +19,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.EnumMap; +import java.util.EnumSet; import java.util.Map; import org.apache.commons.logging.Log; @@ -203,14 +204,14 @@ } /** * - * @param url + * @param url * @param values - * @return <code>true</code> if link of feed has changed, <code>false</code> otherwise. + * @return the set of modified fields. * @throws java.io.IOException * @throws com.sun.syndication.io.FeedException * @throws java.text.ParseException */ - public boolean updateFeed(URL url,Map<Field,Object> values) throws IOException, FeedException, ParseException { + public EnumSet<Field> updateFeedFile(URL url,Map<Field,Object> values) throws IOException, FeedException, ParseException { if (url == null) { throw new NullPointerException("can not add a feed's entry with null url"); } @@ -228,18 +229,12 @@ FileLock lock = acquireLock(f); try { - String newURL =(String) values.get(Field.LINK); SyndFeed feed =RSSIOUtil.readFeed(url); - String oldURL =feed.getLink(); + // update feed and keep trace of modified fields + EnumSet<Field> modifieds = RSSIOUtil.updateFeed(feed,feedProperties, values); - boolean linkChanged = !oldURL.equals(newURL); - //TODO Should be able to detect if at least one thing has changed... - - // get updated nfeed - feed = RSSIOUtil.updateFeed(feed,feedProperties, values); - // save feed into a tmp file File tmpFile = new File(f.getAbsolutePath() + "-tmp_" + System.nanoTime()); @@ -249,7 +244,7 @@ tmpFile.renameTo(f); // feed link has changed - return linkChanged; + return modifieds; } finally { releaseLock(f, lock); } Modified: trunk/lutinrss/src/main/java/org/codelutin/rss/RSSIOUtil.java =================================================================== --- trunk/lutinrss/src/main/java/org/codelutin/rss/RSSIOUtil.java 2008-07-01 12:46:09 UTC (rev 92) +++ trunk/lutinrss/src/main/java/org/codelutin/rss/RSSIOUtil.java 2008-07-02 09:28:07 UTC (rev 93) @@ -18,6 +18,7 @@ import java.net.URL; import java.text.ParseException; import java.util.EnumMap; +import java.util.EnumSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -59,7 +60,7 @@ SyndFeedOutput output = new SyndFeedOutput(); output.output(feed, file); } - + /** * save a feed into a writer. * @param writer @@ -76,19 +77,18 @@ SyndFeed feed = new SyndFeedImpl(); feed.setFeedType(type.getType()); feed.setEncoding("utf-8"); - fillFeed(values, feedProperties, feed); + fillFeed(values, feedProperties, feed, false); return feed; } - - public static SyndFeed updateFeed(URL url,EnumMap<Field, String> feedProperties, Map<Field, Object> values) throws ParseException, IllegalArgumentException, FeedException, IOException { + + public static EnumSet<Field> updateFeed(URL url, EnumMap<Field, String> feedProperties, Map<Field, Object> values) throws ParseException, IllegalArgumentException, FeedException, IOException { SyndFeed feed = RSSIOUtil.readFeed(url); - updateFeed(feed, feedProperties, values); - return feed; + return updateFeed(feed, feedProperties, values); } - - public static SyndFeed updateFeed(SyndFeed feed ,EnumMap<Field, String> feedProperties, Map<Field, Object> values) throws ParseException, IllegalArgumentException, FeedException, IOException { - fillFeed(values, feedProperties, feed); - return feed; + + public static EnumSet<Field> updateFeed(SyndFeed feed, EnumMap<Field, String> feedProperties, Map<Field, Object> values) throws ParseException, IllegalArgumentException, FeedException, IOException { + EnumSet<Field> modifieds = fillFeed(values, feedProperties, feed, true); + return modifieds; } @SuppressWarnings({"unchecked"}) @@ -143,7 +143,8 @@ return feedEntry; } - protected static void fillFeed(Map<Field, Object> values, EnumMap<Field, String> feedProperties, SyndFeed feed) throws ParseException { + protected static EnumSet<Field> fillFeed(Map<Field, Object> values, EnumMap<Field, String> feedProperties, SyndFeed feed, boolean treateModfied) throws ParseException { + EnumSet<Field> modifieds = EnumSet.noneOf(Field.class); for (Entry<Field, Object> entry : values.entrySet()) { Field field = entry.getKey(); @@ -179,9 +180,21 @@ default: realValue = value; } - + if (treateModfied) { + Object oldValue = getFieldValue(feed, name); + if (oldValue == null) { + if (realValue != null) { + modifieds.add(field); + } + } else { + if (!oldValue.equals(realValue)) { + modifieds.add(field); + } + } + } setFieldValue(feed, name, realValue); } + return modifieds; } protected static void setFieldValue(Object dst, String name, Object value) { @@ -197,6 +210,15 @@ } } + protected static Object getFieldValue(Object dst, String name) { + try { + return BeanUtils.getProperty(dst, name); + } catch (Exception ex) { + log.warn("could not access property " + name, ex); + return null; + } + } + protected RSSIOUtil() { // no instance }