Skip to content

Commit

Permalink
Add CommitEventEntity. fixes #350 @50m
Browse files Browse the repository at this point in the history
  • Loading branch information
みぞ@CrazyBeatCoder committed Mar 1, 2018
1 parent 23b5808 commit bb80b7e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 6 deletions.
2 changes: 1 addition & 1 deletion komeiji-bot.iml
Expand Up @@ -17,7 +17,7 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.appengine:appengine-api-1.0-sdk:1.9.60" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.objectify:objectify:5.1.21" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.objectify:objectify:5.1.22" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:20.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.4" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.7" level="project" />
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -36,7 +36,7 @@ Copyright 2015 Google Inc.
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>5.1.21</version>
<version>5.1.22</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -125,7 +125,7 @@ Copyright 2015 Google Inc.
<version>0.39</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<!-- for hot reload of the web application -->
Expand Down
Expand Up @@ -5,10 +5,12 @@
import com.mizo0203.komeiji.domain.UseCase;
import com.mizo0203.komeiji.domain.difine.GitHubUser;
import com.mizo0203.komeiji.domain.difine.TwitterUser;
import com.mizo0203.komeiji.repo.OfyRepository;
import com.mizo0203.komeiji.repo.github.data.Comment;
import com.mizo0203.komeiji.repo.github.data.Commit;
import com.mizo0203.komeiji.repo.github.data.CommitCommentEvent;
import com.mizo0203.komeiji.repo.github.data.PushEvent;
import com.mizo0203.komeiji.repo.objectify.entity.CommitEventEntity;
import org.apache.commons.io.IOUtils;
import twitter4j.Status;

Expand Down Expand Up @@ -97,14 +99,15 @@ private void onPushEvent(String body) throws IOException {
useCase.updateStatus(
TwitterUser.MIZO0203, new CommitMessageTweetFormat().format(commit));
if (status != null) {
storeCommitEvent(status.getId(), commit.getId(), pushEvent.getRepository().getName());
storeCommitEvent(status.getId(), pushEvent.getRepository().getName(), commit.getId());
}
}
}
}

private void storeCommitEvent(long statusId, String commitId, String repositoryName) {
// TODO
private void storeCommitEvent(long statusId, String repositoryName, String commitId) {
OfyRepository.getInstance()
.saveKeyEntity(new CommitEventEntity(statusId, repositoryName, commitId));
}

@Override
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/mizo0203/komeiji/repo/OfyRepository.java
@@ -0,0 +1,18 @@
package com.mizo0203.komeiji.repo;

import com.googlecode.objectify.ObjectifyService;
import com.mizo0203.komeiji.repo.objectify.entity.CommitEventEntity;

public class OfyRepository {
private static final OfyRepository ourInstance = new OfyRepository();

private OfyRepository() {}

public static OfyRepository getInstance() {
return ourInstance;
}

public void saveKeyEntity(CommitEventEntity entity) {
ObjectifyService.ofy().save().entity(entity).now();
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/mizo0203/komeiji/repo/objectify/OfyHelper.java
@@ -0,0 +1,27 @@
package com.mizo0203.komeiji.repo.objectify;

import com.googlecode.objectify.ObjectifyService;
import com.mizo0203.komeiji.repo.objectify.entity.CommitEventEntity;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* OfyHelper, a ServletContextListener, is setup in web.xml to run before a JSP is run. This is
* required to let JSP's access OfyRepository.
*/
public class OfyHelper implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent event) {
// This will be invoked as part of a warmup request, or the first user
// request if no warmup
// request.
ObjectifyService.register(CommitEventEntity.class);
}

@Override
public void contextDestroyed(ServletContextEvent event) {
// App Engine does not currently invoke this method.
}
}
@@ -0,0 +1,37 @@
package com.mizo0203.komeiji.repo.objectify.entity;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.mizo0203.komeiji.repo.objectify.OfyHelper;

/**
* The @Entity tells Objectify about our entity. We also register it in {@link OfyHelper} Our
* primary key @Id is set automatically by the Google Datastore for us.
*
* <p>We add a @Parent to tell the object about its ancestor. We are doing this to support many
* guestbooks. Objectify, unlike the AppEngine library requires that you specify the fields you want
* to index using @Index. Only indexing the fields you need can lead to substantial gains in
* performance -- though if not indexing your data from the start will require indexing it later.
*
* <p>NOTE - all the properties are PUBLIC so that can keep the code simple.
*/
@SuppressWarnings({"FieldCanBeLocal", "unused"})
@Entity
public class CommitEventEntity {

@Id private long statusId;

private String repositoryName;

private String commitId;

public CommitEventEntity() {
// CommitEventEntity must have a no-arg constructor
}

public CommitEventEntity(long statusId, String repositoryName, String commitId) {
this.statusId = statusId;
this.repositoryName = repositoryName;
this.commitId = commitId;
}
}
13 changes: 13 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Expand Up @@ -89,6 +89,19 @@
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>

<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.mizo0203.komeiji.repo.objectify.OfyHelper</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
Expand Down

0 comments on commit bb80b7e

Please sign in to comment.