diff --git a/Jenkinsfile b/Jenkinsfile index 397c402..78aef3c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,5 +1,5 @@ -node ('test'){ - stage ('checkout') { +stage('checkout'){ + steps { checkout scm } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 23173ae..c220a9a 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,10 @@ id 'java' id 'war' } +war { + enabled = true + archiveName 'cosmos.war' +} apply plugin: 'io.spring.dependency-management' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 44e7c4d..c0b51b4 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Thu Jun 06 17:37:50 JST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip diff --git a/src/main/java/com/example/cosmos_serversb/JerseyConfig.java b/src/main/java/com/example/cosmos_serversb/JerseyConfig.java index db39ee6..37ef660 100644 --- a/src/main/java/com/example/cosmos_serversb/JerseyConfig.java +++ b/src/main/java/com/example/cosmos_serversb/JerseyConfig.java @@ -5,11 +5,8 @@ import org.springframework.stereotype.Component; @Component -@ApplicationPath("/cosmos") public class JerseyConfig extends ResourceConfig { - public JerseyConfig() { packages("com.example.cosmos_serversb.resources"); } - } \ No newline at end of file diff --git a/src/main/java/com/example/cosmos_serversb/entities/Group.java b/src/main/java/com/example/cosmos_serversb/entities/Group.java index dfff30a..b1d1dcb 100644 --- a/src/main/java/com/example/cosmos_serversb/entities/Group.java +++ b/src/main/java/com/example/cosmos_serversb/entities/Group.java @@ -3,10 +3,8 @@ import java.util.ArrayList; -import static com.example.cosmos_serversb.models.Users.*; - public class Group { - private Long gId; + private String gId; private String uri, name; private ArrayList members; private ArrayList requests; @@ -16,16 +14,16 @@ } public Group(String gId, String uri, String name, String uId){ - this.gId = Long.parseLong(gId); - this.uri = uri; - this.name = name; + setgId(gId); + setUri(uri); + setName(name); } - public Long getgId() { + public String getgId() { return gId; } - private void setgId(Long gId) { + private void setgId(String gId) { this.gId = gId; } diff --git a/src/main/java/com/example/cosmos_serversb/entities/Request.java b/src/main/java/com/example/cosmos_serversb/entities/Request.java index 40246a2..db9799c 100644 --- a/src/main/java/com/example/cosmos_serversb/entities/Request.java +++ b/src/main/java/com/example/cosmos_serversb/entities/Request.java @@ -2,16 +2,17 @@ package com.example.cosmos_serversb.entities; public class Request { - private String rId, uri, date, IssuerUid, product, deadline; + private String rId; + private String uri, date, IssuerUid, product, deadline; private int location;// YahooローカルサーチAPIの業種コード private boolean done = false; - private Request(){ + public Request(){ } public Request(String rId, String uri, String date, String IssuerUid, String product, String deadline, int location){ - setRid(rId); + setrId(rId); setUri(uri); setDate(date); setIssuerUid(IssuerUid); @@ -21,12 +22,12 @@ this.done = false; } - private void setRid(String rId){ - this.rId = rId; + public String getrId() { + return rId; } - public String getRid(){ - return rId; + private void setrId(String rId) { + this.rId = rId; } private void setUri(String uri){ diff --git a/src/main/java/com/example/cosmos_serversb/entities/Token.java b/src/main/java/com/example/cosmos_serversb/entities/Token.java new file mode 100644 index 0000000..7b6cabb --- /dev/null +++ b/src/main/java/com/example/cosmos_serversb/entities/Token.java @@ -0,0 +1,8 @@ +package com.example.cosmos_serversb.entities; + +public class Token { + public String token; + public Token(String token){ + this.token =token; + } +} diff --git a/src/main/java/com/example/cosmos_serversb/models/Users.java b/src/main/java/com/example/cosmos_serversb/models/Users.java index 9c07b91..02781c7 100644 --- a/src/main/java/com/example/cosmos_serversb/models/Users.java +++ b/src/main/java/com/example/cosmos_serversb/models/Users.java @@ -65,9 +65,10 @@ return DeleteResult.SUCCESS; } - public static String login(String uId) { + public static Token login(String uId) { + Token testtoken = new Token(getInstance().createToken(uId)); //tokenを作成後値を返す - return getInstance().createToken(uId);//testToken + return testtoken;//testToken } public static DeleteResult logout(String token) { diff --git a/src/main/java/com/example/cosmos_serversb/resources/GroupsRest.java b/src/main/java/com/example/cosmos_serversb/resources/GroupsRest.java index 56e936d..9564c25 100644 --- a/src/main/java/com/example/cosmos_serversb/resources/GroupsRest.java +++ b/src/main/java/com/example/cosmos_serversb/resources/GroupsRest.java @@ -6,30 +6,30 @@ import javax.ws.rs.*; -@Component +import com.example.cosmos_serversb.models.*; +@Component +@Path("/groups") public class GroupsRest { - @Path("/groups") @GET public String getGroupsListByUid(@QueryParam("uId") String uId, @QueryParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().getGroupsListByUid(uId)); return json; } - @Path("/groups") @POST public String createGroup(@FormParam("name") String name, @FormParam("uId") String uId, @FormParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().createGroup(name, uId)); return json; } @Path("/{gId}") @GET - public String getGroupInfoByGid(@PathParam("gId") String gId, @QueryParam("token") String token) throws JsonProcessingException{ + public String getGroupByGid(@PathParam("gId") String gId, @QueryParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().getGroupByGid(gId)); return json; } @@ -37,7 +37,7 @@ @DELETE public String deleteGroup(@PathParam("gId") String gId, @FormParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().deleteGroup(gId)); return json; } @@ -45,7 +45,7 @@ @GET public String getGroupMembersListByGid(@PathParam("gId") String gId, @QueryParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().getMembersListByGid(gId)); return json; } @@ -53,7 +53,7 @@ @POST public String addMember(@PathParam("gId") String gId, @FormParam("uId") String uId, @FormParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().addMember(gId, uId)); return json; } @@ -61,7 +61,7 @@ @DELETE public String deleteMember(@PathParam("gId") String gId, @FormParam("uId") String uId, @FormParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().deleteMember(gId, uId)); return json; } @@ -69,7 +69,7 @@ @GET public String getRequestsListByGid(@PathParam("gId") String gId, @QueryParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().getRequestsListByGid(gId)); return json; } @@ -77,7 +77,7 @@ @POST public String addRequests(@PathParam("gId") String gId, @FormParam("uId") String uId, @FormParam("product") String product, @FormParam("deadline") String deadline, @FormParam("location") int location, @FormParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().addRequests(gId, uId, product, deadline, location)); return json; } @@ -85,7 +85,7 @@ @GET public String getRequestsDetailByGidAndRid(@PathParam("gId") String gId, @PathParam("rId") String rId, @QueryParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().getRequestDetailByGidAndRid(gId, rId)); return json; } @@ -93,7 +93,7 @@ @PUT public String updateRequest(@PathParam("gId") String gId, @PathParam("rId") String rId, @FormParam("uId") String uId, @FormParam("product") String product, @FormParam("deadline") String deadline, @FormParam("location") int location, @FormParam("done") boolean done, @FormParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().updateRequest(gId, rId, uId, product, deadline, location, done)); return json; } @@ -101,7 +101,7 @@ @DELETE public String deleteRequest(@PathParam("gId") String gId, @PathParam("rId") String rId, @FormParam("token") String token) throws JsonProcessingException{ ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writeValueAsString(null); + String json = mapper.writeValueAsString(Groups.getInstance().deleteRequest(gId, rId)); return json; } } diff --git a/src/main/resources/Group.hbm.xml b/src/main/resources/Group.hbm.xml index 8e0333c..aac9a9b 100644 --- a/src/main/resources/Group.hbm.xml +++ b/src/main/resources/Group.hbm.xml @@ -14,7 +14,6 @@ - diff --git a/src/main/resources/Request.hbm.xml b/src/main/resources/Request.hbm.xml new file mode 100644 index 0000000..b08773b --- /dev/null +++ b/src/main/resources/Request.hbm.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index 534c01c..e6d6025 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -35,6 +35,7 @@ create + diff --git a/src/test/java/hibernateTest/entities/Group.java b/src/test/java/hibernateTest/entities/Group.java index 0a0acf8..6eacf5b 100644 --- a/src/test/java/hibernateTest/entities/Group.java +++ b/src/test/java/hibernateTest/entities/Group.java @@ -1,15 +1,12 @@ //このクラスは仮です package hibernateTest.entities; -import com.example.cosmos_serversb.entities.Request; -import com.example.cosmos_serversb.entities.User; - import java.util.ArrayList; public class Group { - private Long gId; + private String gId; private String uri, name; - private ArrayList members; + //private ArrayList members; private ArrayList requests; public Group(){ @@ -17,16 +14,16 @@ } public Group(String gId, String uri, String name, String uId){ - this.gId = Long.parseLong(gId); - this.uri = uri; - this.name = name; + setgId(gId); + setUri(uri); + setName(name); } - public Long getgId() { + public String getgId(){ return gId; } - private void setgId(Long gId) { + private void setgId(String gId){ this.gId = gId; } diff --git a/src/test/java/hibernateTest/entities/Request.java b/src/test/java/hibernateTest/entities/Request.java new file mode 100644 index 0000000..1c84ea4 --- /dev/null +++ b/src/test/java/hibernateTest/entities/Request.java @@ -0,0 +1,94 @@ +//このクラスは仮です +package hibernateTest.entities; + +public class Request { + private String rId; + private String uri, date, IssuerUid, product, deadline; + private int location;// YahooローカルサーチAPIの業種コード + private boolean done = false; + + public Request(){ + + } + + public Request(String rId, String uri, String date, String IssuerUid, String product, String deadline, int location){ + setrId(rId); + setUri(uri); + setDate(date); + setIssuerUid(IssuerUid); + setProduct(product); + setDeadline(deadline); + setLocation(location); + this.done = false; + } + + private void setrId(String rId){ + this.rId = rId; + } + + public String getrId(){ + return rId; + } + + + private void setUri(String uri){ + this.uri = uri; + } + + public String getUri(){ + return uri; + } + + private void setDate(String date){ + this.date = date; + } + + public String getDate() { + return date; + } + + private void setIssuerUid(String IssuerUid){ + this.IssuerUid = IssuerUid; + } + + public String getIssuerUid(){ + return IssuerUid; + } + + public String getProduct() { + return product; + } + + public void setProduct(String product) { + this.product = product; + } + + public String getDeadline() { + return deadline; + } + + public void setDeadline(String deadline) { + this.deadline = deadline; + } + + public int getLocation() { + return location; + } + + public void setLocation(int location) { + this.location = location; + } + + public boolean isDone() { + return done; + } + + public boolean getDone() { + return done; + } + + + public void setDone(boolean done) { + this.done = done; + } +} diff --git a/src/test/java/hibernateTest/models/Main.java b/src/test/java/hibernateTest/models/Main.java index e081e4c..22f26f6 100644 --- a/src/test/java/hibernateTest/models/Main.java +++ b/src/test/java/hibernateTest/models/Main.java @@ -2,6 +2,7 @@ import org.hibernate.SessionFactory; +//このメインクラスを実行すると、Hibernateのテストが行えます。 public class Main { public static void main(String args[]){ NativeApiIllustrationTest test = new NativeApiIllustrationTest(); diff --git a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java index c24bb51..e7ac3f7 100644 --- a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java +++ b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java @@ -26,8 +26,12 @@ import java.util.Date; import java.util.List; +import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable; import hibernateTest.entities.Event; import hibernateTest.entities.Group; +import hibernateTest.entities.Request; +import org.hibernate.Criteria; +import org.hibernate.criterion.Restrictions; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; @@ -65,32 +69,69 @@ sessionFactory.close(); } } catch (Exception e) { - + System.out.println("Exception!"); } } public void testBasicUsage() { - // create a couple of events... + /*データの保存(データベースに投入)*/ Session session = sessionFactory.openSession(); session.beginTransaction(); - session.save( new Group("111", "http://test1", "Taro", "011" ) ); - session.save( new Group("222", "http://test2", "Hanako", "022" ) ); - //session.save( new Event( "Our very first event!", new Date() ) ); - //session.save( new Event( "A follow up event", new Date() ) ); + session.save( new Event( "Our very first event!", new Date() ) ); + session.save( new Event( "A follow up event", new Date() ) ); + session.save( new Group("111", "http://test1", "family", "011" ) ); + session.save( new Group("222", "http://test2", "friend", "022" ) ); + Group newGroup = new Group("333", "http://test3", "school", "033" ); + session.save(newGroup); //インスタンスを引数に渡すこともできます + session.save( new Request("111", "http://test01", "1999.01.01", "011", "cookie", "1999.04.10",011)); + session.save( new Request("222", "http://test02", "1999.02.02", "033", "milk", "1999.04.10",011)); + session.save( new Request("333", "http://test03", "1999.03.03", "033", "ice", "1999.04.10",011)); session.getTransaction().commit(); session.close(); - // now lets pull events from the database and list them -// List result = session.createQuery( "from Event" ).list(); -// for ( Event event : (List) result ) { -// System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); -// } + /*データ(レコード)の取得し削除。*/ + //注意: この例ではデータを取得する際、ID(主キー)を指定しているため、複数のデータが返されることを想定していません。 session = sessionFactory.openSession(); session.beginTransaction(); - List result = session.createQuery( "from Group" ).list(); - for ( Group group : (List) result ) { + //load()メソッドを用いて取得する方法。 + Request deleteRequest = (Request) session.load(Request.class, "222"); + //データの削除 + if(deleteRequest != null){ + session.delete(deleteRequest); + } + session.getTransaction().commit(); + session.close(); + + /*データを取得し更新*/ + session = sessionFactory.openSession(); + session.beginTransaction(); + //createCriteriaを用いて取得する方法。ただし非推奨。 + //http://a4dosanddos.hatenablog.com/entry/2015/03/21/135421 + Criteria criteria = session.createCriteria(Request.class).add(Restrictions.eq("rId", "333")); + Request editRequest = (Request) criteria.uniqueResult(); + //データの更新 + if(editRequest != null) { + editRequest.setProduct("Chocolate ice !!"); + session.update(editRequest); + } + session.getTransaction().commit(); + session.close(); + + /*全件取得と出力*/ + session = sessionFactory.openSession(); + session.beginTransaction(); + List result = session.createQuery( "from Event" ).list(); + for ( Event event : (List) result ) { + System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); + } + List groupResult = session.createQuery( "from Group" ).list(); + for ( Group group : (List) groupResult ) { System.out.println( "Group (" + group.getgId() + ") : " + group.getUri() + ": " + group.getName()); } + List requestResult = session.createQuery( "from Request" ).list(); + for ( Request request : (List) requestResult ) { + System.out.println( "Request (" + request.getrId() + ") : " + request.getUri() + ": " + request.getProduct()); + } session.getTransaction().commit(); session.close(); } diff --git a/src/test/resources/Event.hbm.xml b/src/test/resources/Event.hbm.xml index bceae94..047e592 100644 --- a/src/test/resources/Event.hbm.xml +++ b/src/test/resources/Event.hbm.xml @@ -14,7 +14,7 @@ - + diff --git a/src/test/resources/Group.hbm.xml b/src/test/resources/Group.hbm.xml index 8e0333c..7a4a298 100644 --- a/src/test/resources/Group.hbm.xml +++ b/src/test/resources/Group.hbm.xml @@ -10,11 +10,10 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + - diff --git a/src/test/resources/Request.hbm.xml b/src/test/resources/Request.hbm.xml new file mode 100644 index 0000000..d41866b --- /dev/null +++ b/src/test/resources/Request.hbm.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/resources/hibernate.cfg.xml b/src/test/resources/hibernate.cfg.xml index fa1080d..f590b6b 100644 --- a/src/test/resources/hibernate.cfg.xml +++ b/src/test/resources/hibernate.cfg.xml @@ -34,8 +34,10 @@ create - + + +