From 12b45a3fd41774166061b05c49ea4770e8978ef7 Mon Sep 17 00:00:00 2001 From: Bojnourdi Date: Thu, 13 Aug 2015 12:07:07 +0200 Subject: [PATCH] Attribute DAO - Code cleanup & refactoring - Finished client dao --- .../edf/gde/services/AttributesService.java | 2 +- .../com/edf/gde/dao/AttributeDaoClient.java | 66 +++++++++++++++++++ .../GDE-war/test/com/edf/gde/dao/BaseDao.java | 50 +++++++++++++- .../edf/gde/test/dao/AttributeDaoTest.java | 33 +++++++++- 4 files changed, 147 insertions(+), 4 deletions(-) diff --git a/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java b/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java index 3551413..4a05885 100644 --- a/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java +++ b/projects/GDE_App/GDE-war/src/java/com/edf/gde/services/AttributesService.java @@ -22,7 +22,7 @@ import javax.servlet.http.HttpServletResponse; */ public class AttributesService extends BaseService { - public static final String ServiceName = "MetadataService"; + public static final String ServiceName = "AttributesService"; public static final int CREATEATTRIBUTE = 1; public static final int DELETEATTRIBUTE = 2; public static final int READATTRIBUTE = 3; diff --git a/projects/GDE_App/GDE-war/test/com/edf/gde/dao/AttributeDaoClient.java b/projects/GDE_App/GDE-war/test/com/edf/gde/dao/AttributeDaoClient.java index dc90238..2ae2449 100644 --- a/projects/GDE_App/GDE-war/test/com/edf/gde/dao/AttributeDaoClient.java +++ b/projects/GDE_App/GDE-war/test/com/edf/gde/dao/AttributeDaoClient.java @@ -3,10 +3,76 @@ */ package com.edf.gde.dao; +import com.edf.gde.transferables.AttributeGroupTO; +import com.edf.gde.transferables.AttributeTO; +import com.edf.gde.transferables.CommandTO; +import java.io.IOException; +import restapi.RestContext; + /** * * @author Kavoos */ public class AttributeDaoClient extends BaseDao { + + public static final String ServiceName = "AttributesService"; + public static final int CREATEATTRIBUTE = 1; + public static final int DELETEATTRIBUTE = 2; + public static final int READATTRIBUTE = 3; + public static final int CREATEATTRIBUTEGROUP = 4; + public static final int DELETEATTRIBUTEGROUP = 5; + public static final int UPDATEATTRIBUTEGROUP = 6; + public static final int READATTRIBUTEGROUP = 7; + + + public AttributeDaoClient() { + getContext().setBaseResource("http://localhost:8080/GDE-war/AttributesService"); + getContext().setUserName("admin"); + getContext().setPassword("edf123"); + daoResponseHandler = new DaoResponseHandler(); + } + + public AttributeDaoClient(DaoResponseHandler daoResponseHandler, RestContext context) { + super(context); + this.daoResponseHandler = daoResponseHandler; + } + + public AttributeTO createAttribute(AttributeTO ato) throws IOException { + CommandTO commandTO = createCommand(CREATEATTRIBUTE); + return postCommand(commandTO, ato, AttributeTO.class); + } + + public void deleteAttribute(long id) throws IOException { + CommandTO commandTO = createCommand(DELETEATTRIBUTE); + commandTO.setLong("attributeId", id); + postCommand(commandTO, null); + } + + public AttributeTO readAttribute(long id) throws IOException { + CommandTO commandTO = createCommand(READATTRIBUTE); + commandTO.setLong("attributeId", id); + return postCommand(commandTO, null, AttributeTO.class); + } + + public AttributeGroupTO createAttributeGroup(AttributeGroupTO agto) throws IOException { + CommandTO commandTO = createCommand(CREATEATTRIBUTEGROUP); + return postCommand(commandTO, agto, AttributeGroupTO.class); + } + + public void deleteAttributeGroup(long id) throws IOException { + CommandTO commandTO = createCommand(DELETEATTRIBUTEGROUP); + commandTO.setLong("attributeGroupId", id); + postCommand(commandTO, null); + } + + public AttributeGroupTO updateAttributeGroup(AttributeGroupTO agto) throws IOException { + CommandTO commandTO = createCommand(UPDATEATTRIBUTEGROUP); + return postCommand(commandTO, agto, AttributeGroupTO.class); + } + + public AttributeGroupTO readAttributeGroup(long id) throws IOException { + CommandTO commandTO = createCommand(READATTRIBUTEGROUP); + return postCommand(commandTO, null, AttributeGroupTO.class); + } } diff --git a/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java b/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java index 2b83f43..7fb87e0 100644 --- a/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java +++ b/projects/GDE_App/GDE-war/test/com/edf/gde/dao/BaseDao.java @@ -5,6 +5,7 @@ package com.edf.gde.dao; import com.edf.gde.transferables.CommandTO; import com.edf.gde.transferables.responses.CommandResultTO; +import java.io.IOException; import restapi.ResponseHandler; import restapi.RestContext; import restapi.SimpleRestApi; @@ -13,7 +14,9 @@ import restapi.SimpleRestApi; * * @author Kavoos */ -public class BaseDao extends SimpleRestApi { +public abstract class BaseDao extends SimpleRestApi { + + protected DaoResponseHandler daoResponseHandler; public BaseDao() { super(); @@ -69,7 +72,52 @@ public class BaseDao extends SimpleRestApi { resultTO = fromJson(callResponse, CommandResultTO.class); return resultTO; } + } + + /** + * + * @param + * @param commandTO + * @param object + * @param classOf + * @return + * @throws IOException + */ + protected T postCommand(CommandTO commandTO, Object object, Class classOf) throws IOException { + if (object != null) { + commandTO.setData(toJson(object)); + } else { + commandTO = null; + } + if (postAsJSonData(commandTO, daoResponseHandler)) { + CommandResultTO resultTO = daoResponseHandler.getResultTO(); + if (resultTO.getCode() == CommandResultTO.OK) { + T ato = fromJson(resultTO.getData(), classOf); + return ato; + } + } + throw new RuntimeException("Unable to execute command (methode : " + commandTO.getMethod() + ")"); + } + /** + * + * @param commandTO + * @param object + * @throws IOException + */ + protected void postCommand(CommandTO commandTO, Object object) throws IOException { + if (object != null) { + commandTO.setData(toJson(object)); + } else { + commandTO = null; + } + if (postAsJSonData(commandTO, daoResponseHandler)) { + CommandResultTO resultTO = daoResponseHandler.getResultTO(); + if (resultTO.getCode() == CommandResultTO.OK) { + return; + } + } + throw new RuntimeException("Unable to execute command (methode : " + commandTO.getMethod() + ")"); } } diff --git a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java index f3daa01..91fde67 100644 --- a/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java +++ b/projects/GDE_App/GDE-war/test/com/edf/gde/test/dao/AttributeDaoTest.java @@ -3,10 +3,39 @@ */ package com.edf.gde.test.dao; +import com.edf.gde.dao.AttributeDaoClient; +import com.edf.gde.test.base.BaseTest; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + /** * * @author mordicus */ -public class AttributeDaoTest { - +public class AttributeDaoTest extends BaseTest { + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Test + public void testCreateAttribute() throws Exception { + testName("createAttribute"); + AttributeDaoClient daoClient = new AttributeDaoClient(); + } } -- 2.39.2