package
com.mybooks.rest;
import
java.util.List;
import
javax.ws.rs.*;
import
javax.ws.rs.core.MediaType;
import
org.json.JSONObject;
import
com.mybooks.model.Book;
import
com.onenetwork.platform.common.module.ModulePublic;
import
com.onenetwork.platform.data.model.*;
import
com.onenetwork.platform.env.servicelocator.Services;
import
com.onenetwork.platform.integ.json.JSONService;
import
com.onenetwork.platform.integ.rest.BaseResource;
import
com.onenetwork.platform.integ.rest.annotation.*;
@Path
(
"/book"
)
public
class
BookResource
extends
BaseResource {
private
static
final
int
BOOK_SAMPLE_SYS_ID =
10000
;
private
static
final
String BOOK_RESPONSE_SAMPLE_VALUE =
"{\"data\":{\"PublishedDate\":{\"tzId\":\"America/Chicago\",\"date\":\"2010-11-01T15:02:55\",\"tzCode\":\"CDT\"},\"PriceUOM\":\"USD\","
+
"\"PromotionalPeriodStartDate\":{\"tzId\":\"America/Chicago\",\"date\":\"2012-01-01T00:00:00\",\"tzCode\":\"CST\"},\"ISBN\":\"12387111\","
+
"\"PromotionalPeriodEndDate\":{\"tzId\":\"America/Chicago\",\"date\":\"2012-02-01T00:00:00\",\"tzCode\":\"CST\"},\"Author\":\"Greg Merrill\","
+
"\"LastModifiedDate\":{\"tzId\":\"America/Chicago\",\"date\":\"2013-03-24T03:02:49\",\"tzCode\":\"CDT\"},\"ReturnPolicy\":\"P10DT1H30M\",\"ValueChainId\":9123,"
+
"\"LastModifiedUser\":\"DefaultBookstoreValueChainAdmin\",\"PublisherWebsite\":\"<a href=\\\'http:www.onenetwork.com\\\'> One Network Press</a>\",\"AvailableOnline\":true,"
+
"\"PublisherAddress\":{\"STATE\":\"TX\",\"ZIP\":\"75056\",\"COUNTRY\":\"US\",\"CITY\":\"Dallas\",\"STREET1\":\"455 Valley View\"},\"Genre\":\"Non-Fiction\","
+
"\"CreationUser\":\"DefaultBookstoreValueChainAdmin\",\"LevelModifiedDate\":{\"tzId\":\"America/Chicago\",\"date\":\"2013-03-24T03:02:49\",\"tzCode\":\"CDT\"},"
+
"\"CreationDate\":{\"tzId\":\"America/Chicago\",\"date\":\"2013-03-24T03:02:49\",\"tzCode\":\"CDT\"},\"SysId\":10000,\"Title\":\"Learning Platform\",\"PriceAmount\":12.25,"
+
"\"Publisher\":\"One Network Press\",\"Supplier\":{\"value\":10000,\"displayValue\":\"SampleOrg\"},\"State\":\"Proposed\",\"Rating\":1,\"QuantitySold\":3873,"
+
"\"SalesRep\":{\"value\":10005,\"displayValue\":\"BookstoreUser\"},\"PromotionalDiscount\":0.1},\"success\":true}"
;
/**
* Gets a {@link Book}
* @param sysId
* @return
* @throws BookNotFoundException
*/
@ModulePublic
@GET
@Produces
({
"application/json"
})
@RestResponse
(description=
"Returns a book"
, sampleValue=BOOK_RESPONSE_SAMPLE_VALUE,
properties= {
@RestObjectProperty
(name=
"SysId"
, description=
"Unique identifier for a book"
, required=
true
, sampleValue=
"10000"
),
@RestObjectProperty
(name=
"Title"
, description=
"Title of the book"
, required=
true
, sampleValue=
"Learning Platform"
),
@RestObjectProperty
(name=
"ValueChainId"
, description=
"Value Chain identifier"
, required=
true
, sampleValue=
"9123"
),
@RestObjectProperty
(name=
"Author"
, description=
"Author of the book"
, required=
true
, sampleValue=
"Greg Merrill"
),
@RestObjectProperty
(name=
"AvailableOnline"
, description=
"Shows if the book is available online"
, required=
true
, sampleValue=
"true"
),
@RestObjectProperty
(name=
"Genre"
, description=
"<a href=\"#GenreEnum\">Genre</a> of the book"
, required=
true
, sampleValue=
"Non-Fiction"
),
@RestObjectProperty
(name=
"ISBN"
, description=
"ISBN"
, required=
true
),
@RestObjectProperty
(name=
"PriceAmount"
, description=
"Price of the book"
, required=
true
, sampleValue=
"12.25"
),
@RestObjectProperty
(name=
"PriceUOM"
, description=
"Price Unit of Measurement"
, required=
true
, sampleValue=
"USD"
),
@RestObjectProperty
(name=
"PromotionalDiscount"
, description=
"Discount"
, required=
true
, sampleValue=
"0.1"
),
@RestObjectProperty
(name=
"PromotionalPeriodStartDate"
, description=
"Promotional Period Start Data"
, required=
true
),
@RestObjectProperty
(name=
"PromotionalPeriodEndDate"
, description=
"Promotional Period End Data"
, required=
true
),
@RestObjectProperty
(name=
"Publisher"
, description=
"Book Publisher"
, required=
true
, sampleValue=
"One Network Press"
),
@RestObjectProperty
(name=
"PublisherAddress"
, description=
"Publisher Address"
, required=
true
, sampleValue=
"Look at the sample below"
),
@RestObjectProperty
(name=
"QuantitySold"
, description=
"Quantity of books sold"
, required=
true
, sampleValue=
"100"
),
@RestObjectProperty
(name=
"Rating"
, description=
"Book Rating"
, required=
true
, sampleValue=
"1"
),
@RestObjectProperty
(name=
"ReturnPolicy"
, description=
"Return Policy identifier"
, required=
true
, sampleValue=
"P10DT1H30M"
),
@RestObjectProperty
(name=
"State"
, description=
"State"
, required=
true
, sampleValue=
"Proposed"
),
@RestObjectProperty
(name=
"Supplier"
, description=
"Supplier of the book"
, required=
true
)}
)
public
JSONObject getBook(
@QueryParam
(
"SysId"
)
@RestParam
(description=
"Book Identifier"
, required=
true
, sampleValue=
"10000"
) Long sysId)
throws
BookNotFoundException{
ModelDataService mds = Services.get(ModelDataService.
class
);
Book book;
try
{
book = mds.readById(Book.
class
, sysId, getPlatformUserContext());
return
successJson().put(
"data"
, book.toJSONObject());
}
catch
(RuntimeException e) {
throw
new
BookNotFoundException();
}
}
/**
* Gets a book by author and title
*
* @param author
* @param title
* @return
* @throws BookNotFoundException
*/
@ModulePublic
@Path
(
"/{author}"
)
@Produces
(MediaType.APPLICATION_JSON)
@RestResponse
(description=
"Returns a JSON representation of the book"
, sampleValue=BOOK_RESPONSE_SAMPLE_VALUE,
properties= {
@RestObjectProperty
(name=
"SysId"
, description=
"Unique identifier for a book"
, required=
true
, sampleValue=
"10000"
),
@RestObjectProperty
(name=
"Title"
, description=
"Title of the book"
, required=
true
, sampleValue=
"Learning Platform"
),
@RestObjectProperty
(name=
"ValueChainId"
, description=
"Value Chain identifier"
, required=
true
, sampleValue=
"9123"
),
@RestObjectProperty
(name=
"Author"
, description=
"Author of the book"
, required=
true
, sampleValue=
"Greg Merrill"
),
@RestObjectProperty
(name=
"AvailableOnline"
, description=
"Shows if the book is available online"
, required=
true
, sampleValue=
"true"
),
@RestObjectProperty
(name=
"Genre"
, description=
"<a href=\"#GenreEnum\">Genre</a> of the book"
, required=
true
, sampleValue=
"Non-Fiction"
),
@RestObjectProperty
(name=
"ISBN"
, description=
"ISBN"
, required=
true
),
@RestObjectProperty
(name=
"PriceAmount"
, description=
"Price of the book"
, required=
true
, sampleValue=
"12.25"
),
@RestObjectProperty
(name=
"PriceUOM"
, description=
"Price Unit of Measurement"
, required=
true
, sampleValue=
"USD"
),
@RestObjectProperty
(name=
"PromotionalDiscount"
, description=
"Discount"
, required=
true
, sampleValue=
"0.1"
),
@RestObjectProperty
(name=
"PromotionalPeriodStartDate"
, description=
"Promotional Period Start Data"
, required=
true
),
@RestObjectProperty
(name=
"PromotionalPeriodEndDate"
, description=
"Promotional Period End Data"
, required=
true
),
@RestObjectProperty
(name=
"Publisher"
, description=
"Book Publisher"
, required=
true
, sampleValue=
"One Network Press"
),
@RestObjectProperty
(name=
"PublisherAddress"
, description=
"Publisher Address"
, required=
true
, sampleValue=
"Look at the sample below"
),
@RestObjectProperty
(name=
"QuantitySold"
, description=
"Quantity of books sold"
, required=
true
, sampleValue=
"100"
),
@RestObjectProperty
(name=
"Rating"
, description=
"Book Rating"
, required=
true
, sampleValue=
"1"
),
@RestObjectProperty
(name=
"ReturnPolicy"
, description=
"Return Policy identifier"
, required=
true
, sampleValue=
"P10DT1H30M"
),
@RestObjectProperty
(name=
"State"
, description=
"State"
, required=
true
, sampleValue=
"Proposed"
),
@RestObjectProperty
(name=
"Supplier"
, description=
"Supplier of the book"
, required=
true
)}
)
@GET
public
JSONObject getBookByAuthorAndTitle(
@PathParam
(
"author"
)
@RestParam
(description=
"Author of the book"
, required=
true
, sampleValue=
"Greg Merrill"
) String author,
@QueryParam
(
"title"
)
@RestParam
(description=
"Title of the book"
, required=
true
, sampleValue=
"Learning Platform"
) String title)
throws
BookNotFoundException {
ModelDataService mds = Services.get(ModelDataService.
class
);
List<Book> books;
try
{
books = mds.read(Book.
class
, getPlatformUserContext(),
null
, ModelQuery.sqlFilter(
"author = '"
+ author +
"' and title = '"
+ title +
"'"
));
if
(books!=
null
&& !books.isEmpty()) {
return
successJson().put(
"data"
, books.get(
0
).toJSONObject());
}
else
{
return
successJson().put(
"data"
,
"{}"
);
}
}
catch
(RuntimeException e) {
throw
new
BookNotFoundException();
}
}
/**
* Creates a new {@link Book} or updates an existing {@link Book}
* @param bookObj
* @return
*/
@ModulePublic
@POST
@Produces
({
"application/json"
})
@RestResponse
(description=
"Returns the SysId if a new book is created, nothing if existing book is updated"
,
sampleValue=
"{SysId:"
+BOOK_SAMPLE_SYS_ID+
", success:true}"
,
properties={
@RestObjectProperty
(name=
"SysId"
, description=
"Unique identifier for the new book created"
, required=
false
, sampleValue=BOOK_SAMPLE_SYS_ID+
""
)}
)
public
JSONObject createOrUpdateBook(
@FormParam
(
"book"
)
@RestParam
(description=
"A JSON Object representing a book"
, required=
true
) JSONObject bookObj)
throws
BookNotWrittenException {
JSONService js = Services.get(JSONService.
class
);
Book book = js.jsonToModel(bookObj, Book.
class
, getPlatformUserProfile());
String actionName = bookObj.has(
"SysId"
) ?
"ZBKS.Update"
:
"ZBKS.Create"
;
ModelDataService mds = Services.get(ModelDataService.
class
);
ModelList<Book> failedBooks = mds.write(
new
ModelList<Book>(actionName, book), getPlatformUserContext());
if
(failedBooks.getErrors().size() >
0
) {
throw
new
BookNotWrittenException();
}
JSONObject retObj = successJson();
retObj.put(
"SysId"
, book.getSysId());
return
retObj;
}
}