add announcements

This commit is contained in:
bancika
2016-10-26 18:04:52 +02:00
parent d510483f31
commit 8c85e7351c
8 changed files with 249 additions and 9 deletions
@@ -0,0 +1,73 @@
package org.diylc.announcements;
import java.io.Serializable;
public class Announcement implements Serializable {
private static final long serialVersionUID = 1L;
private String title;
private String text;
private String date;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime * result + ((text == null) ? 0 : text.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Announcement other = (Announcement) obj;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (text == null) {
if (other.text != null)
return false;
} else if (!text.equals(other.text))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}
@@ -0,0 +1,64 @@
package org.diylc.announcements;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.diylc.appframework.miscutils.ConfigurationManager;
import org.diylc.plugins.cloud.model.IServiceAPI;
import com.diyfever.httpproxy.PhpFlatProxy;
import com.diyfever.httpproxy.ProxyFactory;
public class AnnouncementProvider {
private String serviceUrl = "http://www.diy-fever.com/diylc/api/v1/announcements.html";
private String LAST_READ_KEY = "announcement.lastReadDate";
private IAnnouncementService service;
private Date lastDate;
private List<Announcement> announcements;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public AnnouncementProvider() {
String lastDateStr = ConfigurationManager.getInstance().readString(LAST_READ_KEY, null);
try {
this.lastDate = lastDateStr == null ? null : dateFormat.parse(lastDateStr);
} catch (ParseException e) {
}
serviceUrl =
ConfigurationManager.getInstance().readString(IServiceAPI.URL_KEY, "http://www.diy-fever.com/diylc/api/v1");
ProxyFactory factory = new ProxyFactory(new PhpFlatProxy());
service = factory.createProxy(IAnnouncementService.class, serviceUrl);
}
public String getCurrentAnnouncements() throws ParseException {
announcements = service.getAnnouncements();
boolean hasUnread = false;
StringBuilder sb = new StringBuilder("<html>");
for (int i = 0; i < announcements.size(); i++) {
Date date = dateFormat.parse(announcements.get(i).getDate());
if (lastDate == null || lastDate.before(date)) {
sb.append("<font size='4'><b>").append(announcements.get(i).getTitle()).append("</b> on ")
.append(announcements.get(i).getDate()).append("</font>").append("<p>")
.append(announcements.get(i).getText()).append("</p>");
hasUnread = true;
}
}
if (!hasUnread)
return "";
sb.append("<br><font size='small'>Click OK to dismiss and mark these announcements as read.</font>");
sb.append("</html>");
return sb.toString();
}
public void dismissed() {
Date date = new Date();
ConfigurationManager.getInstance().writeValue(LAST_READ_KEY, dateFormat.format(date));
}
}
@@ -0,0 +1,8 @@
package org.diylc.announcements;
import java.util.List;
public interface IAnnouncementService {
public List<Announcement> getAnnouncements();
}
@@ -224,7 +224,7 @@ public class CloudPresenter {
if (res instanceof List<?>) {
@SuppressWarnings("unchecked")
List<ProjectEntity> projects = (List<ProjectEntity>) res;
LOG.info("Received " + projects.size() + " results");
LOG.info("Received " + projects.size() + " results. Downloading thumbnails...");
// Download thumbnails and replace urls with local paths to speed up loading in the main thread
for (int i = 0; i < projects.size(); i++) {
String url = projects.get(i).getThumbnailUrl();
@@ -236,6 +236,7 @@ public class CloudPresenter {
projects.get(i).setThumbnailUrl(temp.getAbsolutePath());
fos.close();
}
LOG.info("Finished downloading thumbnails.");
return projects;
}
throw new CloudException("Unexpected server response received for search results: " + res.getClass().getName());