2013-03-20 20:33:42 +01:00
|
|
|
package com.commafeed.backend;
|
|
|
|
|
|
2013-03-30 09:22:49 +01:00
|
|
|
import java.util.Arrays;
|
2013-04-03 16:24:11 +02:00
|
|
|
import java.util.Calendar;
|
2013-03-30 09:22:49 +01:00
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import javax.ejb.Singleton;
|
|
|
|
|
import javax.ejb.Startup;
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-03-21 16:22:58 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
import com.commafeed.backend.dao.ApplicationSettingsDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedCategoryDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
|
|
|
|
import com.commafeed.backend.dao.UserDAO;
|
2013-04-11 19:11:27 +02:00
|
|
|
import com.commafeed.backend.feeds.FeedRefreshWorker;
|
2013-04-05 16:31:42 +02:00
|
|
|
import com.commafeed.backend.model.ApplicationSettings;
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.Feed;
|
|
|
|
|
import com.commafeed.backend.model.FeedCategory;
|
|
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
|
|
|
|
import com.commafeed.backend.model.User;
|
2013-03-30 19:06:32 +01:00
|
|
|
import com.commafeed.backend.model.UserRole.Role;
|
2013-04-11 20:49:08 +02:00
|
|
|
import com.commafeed.backend.services.PasswordEncryptionService;
|
|
|
|
|
import com.commafeed.backend.services.UserService;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Startup
|
|
|
|
|
@Singleton
|
|
|
|
|
public class StartupBean {
|
|
|
|
|
|
2013-03-21 16:22:58 +01:00
|
|
|
private static Logger log = LoggerFactory.getLogger(StartupBean.class);
|
2013-03-30 11:57:11 +01:00
|
|
|
public static final String ADMIN_NAME = "admin";
|
2013-03-21 16:22:58 +01:00
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
@Inject
|
2013-04-11 20:49:08 +02:00
|
|
|
FeedDAO feedDAO;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Inject
|
2013-04-11 20:49:08 +02:00
|
|
|
FeedCategoryDAO feedCategoryDAO;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Inject
|
2013-04-11 20:49:08 +02:00
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
UserDAO userDAO;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
UserService userService;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
PasswordEncryptionService encryptionService;
|
|
|
|
|
|
2013-04-05 16:31:42 +02:00
|
|
|
@Inject
|
2013-04-11 20:49:08 +02:00
|
|
|
ApplicationSettingsDAO applicationSettingsDAO;
|
2013-04-05 16:31:42 +02:00
|
|
|
|
2013-04-11 19:11:27 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedRefreshWorker worker;
|
|
|
|
|
|
2013-04-03 16:24:11 +02:00
|
|
|
private long startupTime;
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
@PostConstruct
|
|
|
|
|
private void init() {
|
2013-04-03 16:24:11 +02:00
|
|
|
startupTime = Calendar.getInstance().getTimeInMillis();
|
2013-04-11 20:49:08 +02:00
|
|
|
if (userDAO.getCount() == 0) {
|
2013-04-11 19:11:27 +02:00
|
|
|
initialData();
|
|
|
|
|
}
|
2013-03-22 11:42:25 +01:00
|
|
|
|
2013-04-11 19:11:27 +02:00
|
|
|
// 3 threads
|
2013-04-11 19:35:16 +02:00
|
|
|
for (int i = 0; i < 6; i++) {
|
2013-04-11 19:11:27 +02:00
|
|
|
worker.start();
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-11 19:11:27 +02:00
|
|
|
private void initialData() {
|
|
|
|
|
log.info("Populating database with default values");
|
|
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
applicationSettingsDAO.save(new ApplicationSettings());
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
User user = userService.register(ADMIN_NAME, "admin",
|
|
|
|
|
Arrays.asList(Role.ADMIN, Role.USER));
|
|
|
|
|
userService.register("test", "test", Arrays.asList(Role.USER));
|
|
|
|
|
|
|
|
|
|
Feed dilbert = new Feed("http://feed.dilbert.com/dilbert/daily_strip");
|
2013-04-11 20:49:08 +02:00
|
|
|
feedDAO.save(dilbert);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
Feed engadget = new Feed("http://www.engadget.com/rss.xml");
|
2013-04-11 20:49:08 +02:00
|
|
|
feedDAO.save(engadget);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
Feed frandroid = new Feed("http://feeds.feedburner.com/frandroid");
|
2013-04-11 20:49:08 +02:00
|
|
|
feedDAO.save(frandroid);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
FeedCategory newsCategory = new FeedCategory();
|
|
|
|
|
newsCategory.setName("News");
|
|
|
|
|
newsCategory.setUser(user);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedCategoryDAO.save(newsCategory);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
FeedCategory comicsCategory = new FeedCategory();
|
|
|
|
|
comicsCategory.setName("Comics");
|
|
|
|
|
comicsCategory.setUser(user);
|
|
|
|
|
comicsCategory.setParent(newsCategory);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedCategoryDAO.save(comicsCategory);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
FeedCategory techCategory = new FeedCategory();
|
|
|
|
|
techCategory.setName("Tech");
|
|
|
|
|
techCategory.setUser(user);
|
|
|
|
|
techCategory.setParent(newsCategory);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedCategoryDAO.save(techCategory);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
FeedSubscription sub = new FeedSubscription();
|
|
|
|
|
sub.setCategory(comicsCategory);
|
|
|
|
|
sub.setFeed(dilbert);
|
|
|
|
|
sub.setTitle("Dilbert - Strips");
|
|
|
|
|
sub.setUser(user);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedSubscriptionDAO.save(sub);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
FeedSubscription sub2 = new FeedSubscription();
|
|
|
|
|
sub2.setCategory(techCategory);
|
|
|
|
|
sub2.setFeed(engadget);
|
|
|
|
|
sub2.setTitle("Engadget");
|
|
|
|
|
sub2.setUser(user);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedSubscriptionDAO.save(sub2);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
|
|
|
|
FeedSubscription sub3 = new FeedSubscription();
|
|
|
|
|
sub3.setFeed(frandroid);
|
|
|
|
|
sub3.setTitle("Frandroid");
|
|
|
|
|
sub3.setUser(user);
|
2013-04-11 20:49:08 +02:00
|
|
|
feedSubscriptionDAO.save(sub3);
|
2013-04-11 19:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-03 16:24:11 +02:00
|
|
|
public long getStartupTime() {
|
|
|
|
|
return startupTime;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|