diff --git a/.gitignore b/.gitignore
index 2c1cc35..ba08868 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,5 +13,5 @@ dist
/.project
/RUNNING_PID
/.settings
-
+test.db
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 01e0494..5ce3e3e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
BookingBug-SDK
BookingBug-SDK
- 1.4
+ 1.9
@@ -68,10 +68,36 @@
commons-lang3
3.0
+
+
+ io.reactivex
+ rxjava
+ 1.1.0
+
+
+
+ org.xerial
+ sqlite-jdbc
+ 3.8.11.2
+
+
+
+ com.j256.ormlite
+ ormlite-jdbc
+ 4.43
+
+
+
+ com.squareup.okhttp3
+ okhttp
+ 3.2.0
+
+
- com.googlecode.json-simple
- json-simple
- 1.1
+ com.squareup.okhttp
+ mockwebserver
+ 2.7.0
+ test
diff --git a/src/main/bookingbugAPI/api/API.java b/src/main/bookingbugAPI/api/API.java
new file mode 100644
index 0000000..358e675
--- /dev/null
+++ b/src/main/bookingbugAPI/api/API.java
@@ -0,0 +1,22 @@
+package bookingbugAPI.api;
+
+/**
+ * Created by sebi on 19.05.2016.
+ */
+public class API extends AbstractAPI {
+
+ public API(ApiConfig config) {
+ super(config);
+ }
+
+ public AdminAPI admin() {
+ return new AdminAPI(newConfig());
+ }
+
+ public static class APIBuilder extends AbstractAPI.ApiConfig {
+
+ public API build() {
+ return new API(this);
+ }
+ }
+}
diff --git a/src/main/bookingbugAPI/api/AbstractAPI.java b/src/main/bookingbugAPI/api/AbstractAPI.java
new file mode 100644
index 0000000..4c7386c
--- /dev/null
+++ b/src/main/bookingbugAPI/api/AbstractAPI.java
@@ -0,0 +1,133 @@
+package bookingbugAPI.api;
+
+import bookingbugAPI.services.CacheService;
+import bookingbugAPI.services.OkHttpService;
+
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Abstract API class
+ * Contains basic methods and members
+ */
+public abstract class AbstractAPI {
+
+ protected OkHttpService httpService;
+
+ public AbstractAPI(ApiConfig config){
+ httpService = new OkHttpService(config);
+ }
+
+ public ApiConfig newConfig() {
+ return new ApiConfig(httpService.getConfig());
+ }
+
+ public String getAuthToken(){
+ return httpService.getConfig().auth_token;
+ }
+
+ public void setAuthToken(String auth_token) {
+ httpService.getConfig().withAuthToken(auth_token);
+ }
+
+ /**
+ * Class which holds an API configuration
+ * @param Keep fluent interface for subclasses
+ */
+ public static class ApiConfig {
+
+ static final String propFileName = "bb_sdk_config.properties";
+ private final static Logger logger = Logger.getLogger(ApiConfig.class.getName());
+
+ public String auth_token;
+ public String appId;
+ public String appKey;
+ public String userAgent;
+ public String serverUrl;
+ public CacheService cacheService;
+
+ public ApiConfig(ApiConfig config) {
+ this.auth_token = config.auth_token;
+ this.appId = config.appId;
+ this.appKey = config.appKey;
+ this.userAgent = config.userAgent;
+ this.serverUrl = config.serverUrl;
+ this.cacheService = config.cacheService;
+ }
+
+ public ApiConfig() {
+ loadConfigFile(null);
+ cacheService = CacheService.JDBC();
+ }
+
+ public T withNothing() {
+ auth_token = null;
+ appId = "";
+ appKey = "";
+ userAgent = "";
+ serverUrl = null;
+ cacheService = null;
+ return (T) this;
+ }
+
+ public T withAuthToken(String token) {
+ this.auth_token = token;
+ return (T)this;
+ }
+
+ public T withCache(CacheService cacheService) {
+ this.cacheService = cacheService;
+ return (T)this;
+ }
+
+ public T withApp(String appId, String appKey) {
+ this.appId = appId;
+ this.appKey = appKey;
+ return (T)this;
+ }
+
+ public T withUserAgent(String userAgent) {
+ this.userAgent = userAgent;
+ return (T)this;
+ }
+
+ public T withServerUrl(String serverUrl) {
+ this.serverUrl = serverUrl;
+ return (T)this;
+ }
+
+ public T withConfigString(String configString) {
+ loadConfigFile(configString);
+ return (T)this;
+ }
+
+ private void loadConfigFile(String configString) {
+ try{
+ Properties prop = new Properties();
+
+ if(configString != null) {
+ prop.load(new StringReader(configString));
+ }
+ else {
+ InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(propFileName);
+ if (inputStream != null) {
+ prop.load(inputStream);
+ } else {
+ throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
+ }
+ }
+
+ appId = prop.getProperty("application.auth.appid");
+ appKey = prop.getProperty("application.auth.appkey");
+ userAgent = prop.getProperty("application.auth.useragent");
+ serverUrl = prop.getProperty("application.auth.serverurl");
+ } catch (Exception e) {
+ logger.log(Level.SEVERE, "Exception @ ApiConfig.withConfigFile(): " + e.getMessage());
+ }
+ }
+ }
+}
diff --git a/src/main/bookingbugAPI/api/AdminAPI.java b/src/main/bookingbugAPI/api/AdminAPI.java
index b610f46..d7809b3 100644
--- a/src/main/bookingbugAPI/api/AdminAPI.java
+++ b/src/main/bookingbugAPI/api/AdminAPI.java
@@ -1,139 +1,37 @@
package bookingbugAPI.api;
import bookingbugAPI.models.*;
+import bookingbugAPI.models.params.BookingListParams;
import bookingbugAPI.services.HttpService;
+import bookingbugAPI.services.OkHttpService;
+import helpers.Utils;
import java.io.IOException;
import java.net.URL;
-public class AdminAPI {
+public class AdminAPI extends AbstractAPI {
- static public class CompanyAPI extends AuthedAPI {
-
- public CompanyAPI(String token) {
- super(token);
- }
-
- public Company companyList(String companyId) throws IOException {
- String uri = AdminURLS.Company.companyList().set("companyId", companyId).expand();
- URL url = new URL(uri);
-
- return new Company(HttpService.api_GET(url, auth_token));
- }
- }
-
- static public class PersonAPI extends AuthedAPI {
-
- public PersonAPI(String token) {
- super(token);
- }
-
- public People personList(String companyId) throws IOException {
- String uri = AdminURLS.Person.personList().set("companyId", companyId).expand();
- URL url = new URL (uri);
-
- return new People(HttpService.api_GET(url, auth_token));
- }
-
- public People personRead(String companyId, String personId) throws IOException {
- String uri = AdminURLS.Person.personRead().set("companyId", companyId).set("personId", personId).expand();
- URL url = new URL (uri);
-
- return new People(HttpService.api_GET(url, auth_token));
- }
-
- public People personGetSchema(String companyId) throws IOException{
- String uri = AdminURLS.Person.personGetSchema().set("companyId", companyId).expand();
- URL url = new URL (uri);
-
- return new People(HttpService.api_GET(url, auth_token));
- }
-
- public People personCreate(String companyId, People person) throws IOException{
- String uri = AdminURLS.Person.personCreate().set("companyId", companyId).expand();
- URL url = new URL (uri);
-
- return new People(HttpService.api_POST(url, person.data, auth_token));
- }
-
- public People personUpdate(String companyId, People person) throws IOException{
- String uri = AdminURLS.Person.personUpdate().set("companyId", companyId).set("personId", person.id).expand();
- URL url = new URL (uri);
-
- return new People(HttpService.api_PUT(url, person.data, auth_token));
- }
-
- public People personDelete(String companyId, String personId) throws IOException{
- String uri = AdminURLS.Person.personDelete().set("companyId", companyId).set("personId", personId).expand();
- URL url = new URL (uri);
-
- return new People(HttpService.api_DELETE(url, auth_token));
- }
+ AdminAPI(ApiConfig builder) {
+ super(builder);
}
- static public class ServiceAPI extends AuthedAPI {
-
- public ServiceAPI(String token) {
- super(token);
- }
-
- public Service serviceList(String companyId) throws IOException {
- String uri = AdminURLS.Service.serviceList().set("companyId", companyId).expand();
- URL url = new URL (uri);
-
- return new Service(HttpService.api_GET(url, auth_token));
- }
- public Service serviceRead(String companyId, String serviceId) throws IOException {
- String uri = AdminURLS.Service.serviceRead().set("companyId", companyId).set("serviceId", serviceId).expand();
- URL url = new URL (uri);
-
- return new Service(HttpService.api_GET(url, auth_token));
- }
+ public BookingAPI booking() {
+ return new BookingAPI(newConfig());
}
+ public class BookingAPI extends AbstractAPI {
- static public class ResourceAPI extends AuthedAPI {
-
- public ResourceAPI(String token) {
- super(token);
- }
-
- public Resource getResources(String companyId) throws IOException {
- String uri = AdminURLS.Resource.resourceList().set("companyId", companyId).expand();
- URL url = new URL (uri);
-
- return new Resource(HttpService.api_GET(url, auth_token));
+ BookingAPI(ApiConfig config) {
+ super(config);
}
- public Resource getResource(String companyId, String resourceId) throws IOException {
- String uri = AdminURLS.Resource.resourceRead().set("companyId", companyId).set("resourceId", resourceId).expand();
- URL url = new URL (uri);
-
- return new Resource(HttpService.api_GET(url, auth_token));
- }
- }
-
-
- static public class EventAPI extends AuthedAPI{
-
- public EventAPI(String token) {
- super(token);
- }
-
- public Event eventList(String companyId) throws IOException {
- String uri = AdminURLS.Event.eventList().set("companyId", companyId).expand();
- URL url = new URL (uri);
-
- return new Event(HttpService.api_GET(url, auth_token));
- }
-
- public Event eventRead(String companyId, String eventId) throws IOException {
- String uri = AdminURLS.Event.eventRead().set("companyId", companyId).set("eventId", eventId).expand();
- URL url = new URL (uri);
-
- return new Event(HttpService.api_GET(url, auth_token));
+ public BBCollection getBookings(Company company, BookingListParams bLParams) throws IOException {
+ URL url = new URL(Utils.inflateLink(company.get_bookingsLink(), bLParams.getParams()));
+ BBCollection bookings = new BBCollection(httpService.api_GET(url), getAuthToken(), "bookings", Booking.class);
+ //BBCollection bookings = new BBCollection(HttpService.api_GET(url, getAuthToken()), getAuthToken(), "bookings", Booking.class);
+ return bookings;
}
}
diff --git a/src/main/bookingbugAPI/api/AdminURLS.java b/src/main/bookingbugAPI/api/AdminURLS.java
index e4ba96e..21141c0 100644
--- a/src/main/bookingbugAPI/api/AdminURLS.java
+++ b/src/main/bookingbugAPI/api/AdminURLS.java
@@ -11,6 +11,7 @@ public static class Company {
public static UriTemplate company() {
return UriTemplate.buildFromTemplate(new Config().serverUrl)
.literal("/admin")
+ .path(UriTemplateBuilder.var("companyId"))
.literal("/company")
.build();
}
@@ -85,6 +86,7 @@ public static UriTemplate serviceList(){
.literal("/admin")
.path(UriTemplateBuilder.var("companyId"))
.literal("/services")
+ .query(UriTemplateBuilder.var("page"), UriTemplateBuilder.var("per_page"))
.build();
}
@@ -96,6 +98,16 @@ public static UriTemplate serviceRead() {
.path(UriTemplateBuilder.var("serviceId"))
.build();
}
+
+ public static UriTemplate serviceNewBooking() {
+ return UriTemplate.buildFromTemplate(new Config().serverUrl)
+ .literal("/admin")
+ .path(UriTemplateBuilder.var("companyId"))
+ .literal("/services")
+ .path(UriTemplateBuilder.var("serviceId"))
+ .literal("/new_booking")
+ .build();
+ }
}
@@ -269,6 +281,7 @@ public static UriTemplate bookingList(){
.literal("/admin")
.path(UriTemplateBuilder.var("companyId"))
.literal("/bookings")
+ .query(UriTemplateBuilder.var("page"), UriTemplateBuilder.var("per_page"))
.build();
}
@@ -625,6 +638,16 @@ public static UriTemplate eventChainReadUsingRefId() {
.path(UriTemplateBuilder.var("refId"))
.build();
}
+
+ public static UriTemplate eventChainEventsList() {
+ return UriTemplate.buildFromTemplate(new Config().serverUrl)
+ .literal("/admin")
+ .path(UriTemplateBuilder.var("companyId"))
+ .literal("/event_chains")
+ .path(UriTemplateBuilder.var("eventChainId"))
+ .literal("/events")
+ .build();
+ }
}
diff --git a/src/main/bookingbugAPI/api/AuthedAPI.java b/src/main/bookingbugAPI/api/AuthedAPI.java
index b61da4b..a117351 100644
--- a/src/main/bookingbugAPI/api/AuthedAPI.java
+++ b/src/main/bookingbugAPI/api/AuthedAPI.java
@@ -1,12 +1,13 @@
package bookingbugAPI.api;
/**
- * Created by sebi on 5/30/15.
+ * Created by sebi on 19.05.2016.
*/
-public abstract class AuthedAPI {
+public class AuthedAPI {
+
String auth_token;
- public AuthedAPI(String token){
- auth_token = token;
+ public AuthedAPI(String auth_token) {
+ this.auth_token = auth_token;
}
}
diff --git a/src/main/bookingbugAPI/api/PublicURLS.java b/src/main/bookingbugAPI/api/PublicURLS.java
index acfae19..1be7644 100644
--- a/src/main/bookingbugAPI/api/PublicURLS.java
+++ b/src/main/bookingbugAPI/api/PublicURLS.java
@@ -67,7 +67,9 @@ public static class Service {
*/
public static UriTemplate serviceList(){
return UriTemplate.buildFromTemplate(new Config().serverUrl).path(UriTemplateBuilder.var("companyId"))
- .literal("/" + servicesLink).build();
+ .literal("/" + servicesLink)
+ .query(UriTemplateBuilder.var("page"), UriTemplateBuilder.var("per_page"))
+ .build();
}
/**
@@ -179,8 +181,13 @@ public static class Event {
* @return UriTemplate
*/
public static UriTemplate eventList(){
- return UriTemplate.buildFromTemplate(new Config().serverUrl).path(UriTemplateBuilder.var("companyId"))
- .literal("/" + eventsLink).build();
+ return UriTemplate.buildFromTemplate(new Config().serverUrl)
+ .path(UriTemplateBuilder.var("companyId"))
+ .literal("/" + eventsLink)
+ .query("page", "per_page", "event_chain_id",
+ "start_date", "end_date", "resource_id", "person_id",
+ "event_group_id", "summary", "member_level_id")
+ .build();
}
/**
@@ -244,7 +251,9 @@ public static UriTemplate availabilityDaysForBookableItem() {
*/
public static UriTemplate availabilityTimesForBookableItem() {
return UriTemplate.buildFromTemplate(new Config().serverUrl).path(UriTemplateBuilder.var("companyId"))
- .literal("/time_data").build();
+ .literal("/time_data")
+ .query("event_id", "service_id", "resource_id","resource_ids", "person_id", "group_id", "location",
+ "date", "end_date", "duration", "num_resources").build();
}
/**
@@ -314,7 +323,9 @@ public static class EventChain {
*/
public static UriTemplate eventChainList(){
return UriTemplate.buildFromTemplate(new Config().serverUrl).path(UriTemplateBuilder.var("companyId"))
- .literal("/" + eventChainsLink).build();
+ .literal("/" + eventChainsLink)
+ .query(UriTemplateBuilder.var("page"), UriTemplateBuilder.var("per_page"))
+ .build();
}
/**
diff --git a/src/main/bookingbugAPI/models/Address.java b/src/main/bookingbugAPI/models/Address.java
index 2c44b42..f01e559 100644
--- a/src/main/bookingbugAPI/models/Address.java
+++ b/src/main/bookingbugAPI/models/Address.java
@@ -2,7 +2,6 @@
import helpers.HttpServiceResponse;
-
public class Address extends BBRoot{
public Address(HttpServiceResponse httpServiceResponse){
@@ -10,4 +9,130 @@ public Address(HttpServiceResponse httpServiceResponse){
}
public Address() {}
+
+ /**
+ * Returns the id.
+ *
+ * @return the id associated with the current Address object
+ */
+ public Integer getId() {
+ return getInteger("id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the first address.
+ *
+ * @return the first address associated with the current Address object
+ */
+ public String getAddress1() {
+ return get("address1");
+ }
+
+ /**
+ * Returns the second address.
+ *
+ * @return the second address associated with the current Address object
+ */
+ public String getAddress2() {
+ return get("address2");
+ }
+
+ /**
+ * Returns the third address.
+ *
+ * @return the third address associated with the current Address object
+ */
+ public String getAddress3() {
+ return get("address3");
+ }
+
+ /**
+ * Returns the fourth address.
+ *
+ * @return the fourth address associated with the current Address object
+ */
+ public String getAddress4() {
+ return get("address4");
+ }
+
+ /**
+ * Returns the fifth address.
+ *
+ * @return the fifth address associated with the current Address object
+ */
+ public String getAddress5() {
+ return get("address5");
+ }
+
+ /**
+ * Returns the post code.
+ *
+ * @return the post code associated with the current Address object
+ */
+ public String getPostcode() {
+ return get("postcode");
+ }
+
+ /**
+ * Returns the country from the address.
+ *
+ * @return the country associated with the current Address object
+ */
+ public String getCountry() {
+ return get("country");
+ }
+
+ /**
+ * Returns the latitude expressed in degrees.
+ *
+ * @return the latitude associated with the current Address object
+ */
+ public Double getLat() {
+ return getDouble("lat", DOUBLE_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the longitude expressed in degrees.
+ *
+ * @return the longitude associated with the current Address object
+ */
+ public Double getLong() {
+ return getDouble("long", DOUBLE_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the map url.
+ *
+ * @return the map url associated with the current Address object
+ */
+ public String getMapUrl() {
+ return get("map_url");
+ }
+
+ /**
+ * Returns the map marker.
+ *
+ * @return the map marker associated with the current Address object
+ */
+ public String getMapMarker() {
+ return get("map_marker");
+ }
+
+ /**
+ * Returns the phone from address.
+ *
+ * @return the phone associated with the current Address object
+ */
+ public String getPhone() {
+ return get("phone");
+ }
+
+ /**
+ * Returns the home phone from address.
+ *
+ * @return the home phone associated with the current Address object
+ */
+ public String getHomephone() {
+ return get("homephone");
+ }
}
diff --git a/src/main/bookingbugAPI/models/Administrator.java b/src/main/bookingbugAPI/models/Administrator.java
index ad07a1c..9bb8875 100644
--- a/src/main/bookingbugAPI/models/Administrator.java
+++ b/src/main/bookingbugAPI/models/Administrator.java
@@ -11,37 +11,129 @@
public class Administrator extends BBRoot {
- private Company company;
- private BBRoot schema;
-
-
public Administrator(HttpServiceResponse httpServiceResponse, String auth_token) {
super(httpServiceResponse);
this.auth_token = auth_token;
}
+ public Administrator(HttpServiceResponse httpServiceResponse) {
+ super(httpServiceResponse);
+ }
public Company getCompany() throws IOException {
- if(company == null){
- String link = response.getRep().getLinkByRel("company").getHref();
- URL url = new URL(UriTemplate.fromTemplate(link).expand());
- company = new Company(HttpService.api_GET(url, auth_token), auth_token);
- }
- return company;
+ String link = response.getRep().getLinkByRel("company").getHref();
+ URL url = new URL(UriTemplate.fromTemplate(link).expand());
+ return new Company(HttpService.api_GET(url, auth_token), auth_token);
}
public BBRoot getEditSchema() throws IOException {
- if(schema == null) {
- String link = response.getRep().getLinkByRel("edit").getHref();
- URL url = new URL(UriTemplate.fromTemplate(link).expand());
- schema = new BBRoot(HttpService.api_GET(url, auth_token), auth_token);
- }
- return schema;
+ String link = response.getRep().getLinkByRel("edit").getHref();
+ URL url = new URL(UriTemplate.fromTemplate(link).expand());
+ return new BBRoot(HttpService.api_GET(url, auth_token), auth_token);
}
- public Login login(Map params) throws IOException {
+ public Login login(Map params) throws IOException {
return auth(params, response.getRep().getLinkByRel("login"));
}
+
+ /**
+ * Returns the name of the admin.
+ *
+ * @return the name associated with the current Administrator object
+ */
+ public String getName() {
+ return get("name");
+ }
+
+ /**
+ * Returns the email of the admin.
+ *
+ * @return the email associated with the current Administrator object
+ */
+ public String getEmail() {
+ return get("email");
+ }
+
+ /**
+ * Returns the role of the admin.
+ *
+ * @return the associated with the current administrator object
+ */
+ public String getRole() {
+ return get("role");
+ }
+
+ /**
+ * Returns the company id.
+ *
+ * @return the company id associated with the current administrator object
+ */
+ public Integer getCompanyId() {
+ return getInteger("company_id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the company name.
+ *
+ * @return the company name associated with the current administrator object
+ */
+ public String getCompanyName() {
+ return get("company_name");
+ }
+
+ /**
+ * Returns the person id.
+ *
+ * @return the person id associated with the current administrator object
+ */
+ public Integer getPersonId() {
+ return getInteger("person_id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the edit link.
+ *
+ * @return the edit link associated with the current administrator object
+ */
+ public String getEditLink() {
+ return getLink("edit");
+ }
+
+ /**
+ * Returns the company link.
+ *
+ * @return the company link associated with the current administrator object
+ */
+ public String getCompanyLink() {
+ return getLink("company");
+ }
+
+ /**
+ * Returns the person link.
+ *
+ * @return the person link associated with the current administrator object
+ */
+ public String getPersonLink() {
+ return getLink("person");
+ }
+
+ /**
+ * Returns the login link.
+ *
+ * @return the login link associated with the current administrator object
+ */
+ public String getLoginLink() {
+ return getLink("login");
+ }
+
+ /**
+ * Returns the base_login.
+ *
+ * @return the base_login associated with the current administrator object
+ */
+ public String getBaseLoginLink() {
+ return getLink("base_login");
+ }
}
diff --git a/src/main/bookingbugAPI/models/Answer.java b/src/main/bookingbugAPI/models/Answer.java
new file mode 100644
index 0000000..6a1e197
--- /dev/null
+++ b/src/main/bookingbugAPI/models/Answer.java
@@ -0,0 +1,117 @@
+package bookingbugAPI.models;
+
+import helpers.HttpServiceResponse;
+
+public class Answer extends BBRoot{
+
+ public Answer(HttpServiceResponse httpServiceResponse) {
+ super(httpServiceResponse);
+ }
+
+ public Answer(HttpServiceResponse httpServiceResponse, String auth_token) {
+ super(httpServiceResponse, auth_token);
+ }
+
+ public Answer() {
+ }
+
+ /**
+ * Returns the answer's id.
+ *
+ * @return The id associated with the current Answer object.
+ */
+ public Integer getId() {
+ return getInteger("id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the answer's value.
+ *
+ * @return The value associated with the current Answer object.
+ */
+ public String getValue() {
+ return get("value");
+ }
+
+ /**
+ * Returns the answer's price.
+ *
+ * @return The price associated with the current Answer object.
+ */
+ public Double getPrice() {
+ return getDouble("price", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the question id.
+ *
+ * @return The question id associated with the current Answer object.
+ */
+ public Integer getQuestionId() {
+ return getInteger("question_id", INTEGER_DEFAULT_VALUE);
+ }
+
+
+ /**
+ * Returns the admin only attribute.
+ *
+ * @return The admin only attribute associated with the current Answer object.
+ */
+ public Boolean getAdminOnly() {
+ return getBoolean("admin_only", BOOLEAN_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the important attribute.
+ *
+ * @return The important attribute associated with the current Answer object.
+ */
+ public Boolean getImportant() {
+ return getBoolean("important", BOOLEAN_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the question.
+ *
+ * @return The question associated with the current Answer object.
+ */
+ public Question getQuestion() {
+ return new Question(new HttpServiceResponse(getResource("question")));
+ }
+
+ /**
+ * Returns the question text .
+ *
+ * @return The question text associated with the current Answer object.
+ */
+ public String getQuestionText() {
+ return get("question_text");
+ }
+
+ /**
+ * Returns the outcome.
+ *
+ * @return The outcome associated with the current Answer object.
+ */
+ public Boolean getOutcome() {
+ return getBoolean("outcome", BOOLEAN_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the company id.
+ *
+ * @return The company id associated with the current Answer object.
+ */
+ public Integer getCompanyId() {
+ return getInteger("company_id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the question link .
+ *
+ * @return The question link associated with the current Answer object.
+ */
+ public String getQuestionLink() {
+ return getLink("question");
+ }
+}
diff --git a/src/main/bookingbugAPI/models/BBCollection.java b/src/main/bookingbugAPI/models/BBCollection.java
index 89061da..e412ed9 100644
--- a/src/main/bookingbugAPI/models/BBCollection.java
+++ b/src/main/bookingbugAPI/models/BBCollection.java
@@ -83,6 +83,10 @@ public int size() {
return getRep().getResourcesByRel(collectionNameSpace).size();
}
+ public boolean hasNext() {
+ return getLink("next") != null;
+ }
+
public Iterator iterator() {
return new BBCollectionIterator(this);
diff --git a/src/main/bookingbugAPI/models/BBRoot.java b/src/main/bookingbugAPI/models/BBRoot.java
index ac369ea..aabdfca 100644
--- a/src/main/bookingbugAPI/models/BBRoot.java
+++ b/src/main/bookingbugAPI/models/BBRoot.java
@@ -3,16 +3,24 @@
import bookingbugAPI.services.HttpService;
import com.damnhandy.uri.template.UriTemplate;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.theoryinpractise.halbuilder.api.ContentRepresentation;
import com.theoryinpractise.halbuilder.api.Link;
+import com.theoryinpractise.halbuilder.api.ReadableRepresentation;
import com.theoryinpractise.halbuilder.api.RepresentationException;
+import com.theoryinpractise.halbuilder.impl.representations.ContentBasedRepresentation;
import com.theoryinpractise.halbuilder.json.JsonRepresentationFactory;
import helpers.Config;
import helpers.HttpServiceResponse;
+import helpers.hal_addon.CustomJsonDeserializer;
+import org.joda.time.DateTime;
import java.io.*;
import java.net.URL;
-import java.util.*;
+import java.util.List;
+import java.util.Map;
import java.util.logging.Logger;
import static com.theoryinpractise.halbuilder.api.RepresentationFactory.HAL_JSON;
@@ -23,7 +31,7 @@
* Child classes should implement getters for relevant parts from the response.
*/
@JsonIgnoreProperties({
- "rep", "auth_token", "id", "data", "links"
+ "rep", "auth_token", "id", "data", "links"
})
public class BBRoot {
@@ -33,6 +41,9 @@ public class BBRoot {
protected String auth_token = null;
public String id;
public Map data;
+ public int INTEGER_DEFAULT_VALUE = 0;
+ public double DOUBLE_DEFAULT_VALUE = 0.0;
+ public boolean BOOLEAN_DEFAULT_VALUE = false;
protected String curl = "N/A";
@@ -43,7 +54,7 @@ public BBRoot(HttpServiceResponse httpServiceResponse) {
}
- public BBRoot(String auth_token){
+ public BBRoot(String auth_token) {
this.auth_token = auth_token;
}
@@ -55,17 +66,18 @@ public BBRoot(HttpServiceResponse httpServiceResponse, String auth_token) {
}
- public BBRoot() {}
+ public BBRoot() {
+ }
public BBRoot getLoginSchema() throws IOException {
- URL url = new URL (UriTemplate.fromTemplate(response.getRep().getLinkByRel("new_login").getHref()).expand());
+ URL url = new URL(UriTemplate.fromTemplate(response.getRep().getLinkByRel("new_login").getHref()).expand());
HttpServiceResponse response = HttpService.api_GET(url);
return new BBRoot(response);
}
- public Login auth(Map params) throws IOException{
+ public Login auth(Map params) throws IOException {
HttpServiceResponse resp;
try {
URL url = new URL(UriTemplate.fromTemplate(new Config().serverUrl + "/login").expand());
@@ -89,24 +101,98 @@ public Login auth(Map params) throws IOException{
}
- public Login auth(Map params, Link link) throws IOException{
- URL url = new URL (link.getHref());
+ public Login auth(Map params, Link link) throws IOException {
+ URL url = new URL(link.getHref());
HttpServiceResponse resp = HttpService.api_POST(url, params);
auth_token = (String) resp.getRep().getValue("auth_token");
return new Login(resp);
}
- public String get(String key){
+ public String get(String key) {
String val = null;
- try{
- val = (String)response.getRep().getValue(key);
+ try {
+ val = (String) response.getRep().getValue(key);
} catch (RepresentationException e) {
//e.printStackTrace();
}
return val;
}
+ public boolean getBoolean(String key, boolean defaultValue) {
+ String val = this.get(key);
+ if (val != null) return Boolean.parseBoolean(val);
+ return defaultValue;
+ }
+
+ public int getInteger(String key, int defaultValue) {
+ String val = this.get(key);
+ if (val != null) return Integer.parseInt(val);
+ return defaultValue;
+ }
+
+ public double getDouble(String key, double defaultValue) {
+ String val = this.get(key);
+ if (val != null) return Double.parseDouble(val);
+ return defaultValue;
+ }
+
+ public DateTime getDate(String key) {
+ return new DateTime(get(key));
+ }
+
+ public List getArray(String key) {
+ List val = null;
+ try {
+ val = (List) (response.getRep().getValue(key));
+ } catch (RepresentationException e) {
+ e.printStackTrace();
+ }
+ return val;
+ }
+
+ //TODO: improve this
+ public T getObject(String key, Class type) {
+
+ try {
+ ObjectMapper mapper = CustomJsonDeserializer.getMapper();
+ String json_obj = mapper.writeValueAsString(response.getRep().getValue(key));
+ return mapper.readValue(json_obj, type);
+ } catch (RepresentationException | IOException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public List getObjects(String key, Class type) {
+ List val = null;
+
+ try {
+ ObjectMapper mapper = CustomJsonDeserializer.getMapper();
+ String json_obj = mapper.writeValueAsString(response.getRep().getValue(key));
+ val = mapper.readValue(json_obj, new TypeReference>() {
+ });
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return val;
+ }
+
+ //TODO: fix this
+ public ContentRepresentation getResource(String key) {
+ try {
+ List extends ReadableRepresentation> entries = response.getRep().getResourcesByRel(key);
+
+ for (ReadableRepresentation item : entries) {
+ if (item instanceof ContentBasedRepresentation)
+ return (ContentRepresentation) item;
+ }
+ } catch (RepresentationException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
public String getAuth_token() {
return auth_token;
@@ -138,6 +224,16 @@ public String toString() {
return response.getRep().getContent();
}
+ public String toPrettyString() {
+ ObjectMapper mapper = new ObjectMapper();
+ try {
+ return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(response.getRep());
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ return toString();
+ }
+ }
+
public String getCurl() {
curl = "curl \"" + getSelf() + "\" " + response.getParamsStr() + " -X " + response.getMethod();
@@ -146,7 +242,7 @@ public String getCurl() {
public String getSelf() {
- if(response.getRep().getResourceLink() != null)
+ if (response.getRep().getResourceLink() != null)
return response.getRep().getResourceLink().getHref();
return "";
}
diff --git a/src/main/bookingbugAPI/models/Basket.java b/src/main/bookingbugAPI/models/Basket.java
new file mode 100644
index 0000000..58f70b9
--- /dev/null
+++ b/src/main/bookingbugAPI/models/Basket.java
@@ -0,0 +1,80 @@
+package bookingbugAPI.models;
+
+import helpers.HttpServiceResponse;
+
+import java.util.List;
+
+
+public class Basket extends BBRoot{
+
+ public Basket(HttpServiceResponse httpServiceResponse) {
+ super(httpServiceResponse);
+ }
+
+ public String getCompanyId() {
+ return get("company_id");
+ }
+
+ /**
+ * Returns the total price of the basket.
+ *
+ * @return The total price associated with the current Basket object
+ */
+ public Integer getTotalPrice() {
+ return getInteger("total_price", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the total due price of the basket.
+ *
+ * @return The total due price associated with the current Basket object
+ */
+ public Integer getTotalDuePrice() {
+ return getInteger("total_due_price", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the items from the basket.
+ *
+ * @return {@link BBCollection} The items associated with the current Basket object
+ */
+ public BBCollection getItems() {
+ return new BBCollection<>(new HttpServiceResponse(getResource("items")), auth_token, BasketItem.class);
+ }
+
+ /**
+ * Returns the checkout link.
+ *
+ * @return The checkout link associated with the current Basket object
+ */
+ public String getCheckoutLink() {
+ return getLink("checkout");
+ }
+
+ /**
+ * Returns the items link.
+ *
+ * @return The items link associated with the current Basket object
+ */
+ public String getItemsLink() {
+ return getLink("items");
+ }
+
+ /**
+ * Returns the link for adding an item.
+ *
+ * @return The link for adding an item associated with the current Basket object
+ */
+ public String getAddItemLink() {
+ return getLink("add_item");
+ }
+
+ /**
+ * Returns the deal link.
+ *
+ * @return The deal link associated with the current Basket object
+ */
+ public String getDealLink() {
+ return getLink("deal");
+ }
+}
diff --git a/src/main/bookingbugAPI/models/BasketItem.java b/src/main/bookingbugAPI/models/BasketItem.java
new file mode 100644
index 0000000..bf837c0
--- /dev/null
+++ b/src/main/bookingbugAPI/models/BasketItem.java
@@ -0,0 +1,205 @@
+package bookingbugAPI.models;
+
+import bookingbugAPI.api.PublicURLS;
+import bookingbugAPI.services.HttpService;
+import com.theoryinpractise.halbuilder.api.ContentRepresentation;
+import helpers.HttpServiceResponse;
+import org.joda.time.DateTime;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.List;
+import java.util.Map;
+
+
+public class BasketItem extends BBRoot {
+
+
+ public BasketItem(HttpServiceResponse httpServiceResponse) {
+ super(httpServiceResponse);
+ }
+
+ /**
+ * Returns the event_id.
+ *
+ * @return The event_id associated with the current BasketItemsTwo object
+ */
+ public Integer getEventId() {
+ return getInteger("event_id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the person id.
+ *
+ * @return The person id associated with the current BasketItemsTwo object
+ */
+ public Integer getPersonId() {
+ return getInteger("person_id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the service id.
+ *
+ * @return The service id associated with the current BasketItemsTwo object
+ */
+ public Integer getServiceId() {
+ return getInteger("service_id", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the price.
+ *
+ * @return The price associated with the current BasketItemsTwo object
+ */
+ public Integer getPrice() {
+ return getInteger("price", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the number of bookings.
+ *
+ * @return The number of bookings associated with the current BasketItemsTwo object
+ */
+ public Integer getNumBook() {
+ return getInteger("num_book", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the person's name.
+ *
+ * @return The person's name associated with the current BasketItemsTwo object
+ */
+ public String getPersonName() {
+ return get("person_name");
+ }
+
+ /**
+ * Returns the service's name.
+ *
+ * @return The service's name associated with the current BasketItemsTwo object
+ */
+ public String getServiceName() {
+ return get("service_name");
+ }
+
+ /**
+ * Returns the status.
+ *
+ * @return The status associated with the current BasketItemsTwo object
+ */
+ public Integer getStatus() {
+ return getInteger("status", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the id.
+ *
+ * @return The id associated with the current BasketItemsTwo object
+ */
+ public String getId() {
+ return get("id");
+ }
+
+ /**
+ * Returns the date and time, with {@link DateTime DateTime()} as format.
+ *
+ * @return The date and time associated with the current BasketItemsTwo object
+ */
+ public DateTime getDate() {
+ return getDate("date");
+ }
+
+ /**
+ * Returns the time.
+ *
+ * @return The time associated with the current BasketItemsTwo object
+ */
+ public Integer getTime() {
+ return getInteger("time", INTEGER_DEFAULT_VALUE);
+ }
+
+ /**
+ * Returns the questions.
+ *
+ * @return The questions associated with the current BasketItemsTwo object
+ */
+ public List