9124 Rohan Gupta WS CA2
9124 Rohan Gupta WS CA2
(Autonomous)
NEW PANVEL
PROJECT REPORT ON
“OnlineBookstoreAPI”
IN PARTIAL FULFILLMENT OF
SEMESTER V - 2024-25
PROJECT GUIDE
ROLL NO : 9124
Overview
Purpose: The Book class represents an entity within the Online Bookstore API, designed to manage and
store information about books. It serves as a data model for the bookstore application, enabling the
persistence and retrieval of book-related data.
Key Features:
Entity Management:
Annotated with @Entity, allowing it to be used with Java Persistence API (JPA) to handle database
operations.
Attributes:
Provides methods to access and modify the private fields, adhering to encapsulation principles.
Contains equals and hashCode methods to compare Book instances and manage collections.
SWORT :
Strengths:
Entity Annotation:
The class is properly annotated with @Entity, making it suitable for persistence with JPA (Java
Persistence API).
Serializable:
Provides standard getter and setter methods for accessing and modifying private fields,
promoting encapsulation.
XML Binding:
Annotated with @XmlRootElement, allowing the class to be easily converted to and from XML,
which is useful for web services.
Weaknesses:
The fields have inconsistent naming conventions (Author, Title, Bname, Price, Quantity). For clarity
and consistency, use standard naming conventions (e.g., author, title, bookName, price,
quantity).
Lack of Validation:
No validation logic is present for fields like Price and Quantity. Adding validation could prevent
invalid data from being persisted.
The equals and hashCode methods are implemented but might not be fully reliable if the id field is not
set. Consider refining these methods if necessary.
No Default Constructor:
Although not explicitly mentioned, ensure that there is a no-argument constructor, which is typically
required by JPA and many other frameworks.
Opportunities:
Implement validation annotations (e.g., @NotNull, @Min, @Max) to ensure data integrity.
Enhanced Methods:
Add business logic or utility methods that could be useful, such as methods for calculating discounts or
checking stock availability.
Extend the class to support additional features like integration with external APIs for book information or
price updates.
Refactor the field names to follow common Java naming conventions, which will enhance code
readability and maintainability.
Threats:
Without proper validation and constraints, there is a risk of storing invalid or inconsistent data.
Scalability Concerns:
If the Book class is to be part of a larger system, performance and scalability issues could arise if not
handled properly, particularly with large datasets.
Security Risks:
Ensure that fields such as Price and Quantity are properly managed to avoid security vulnerabilities,
such as incorrect pricing or unauthorized changes.
Future Changes:
Changes in the requirements or the data model might necessitate updates to this class, which could
introduce bugs or require extensive refactoring.
In summary, the Book class has a solid foundation but could benefit from improvements in field naming,
validation, and additional features. Addressing these areas can enhance its robustness and adaptability in
an evolving application environment.
Code :
Book.java
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package abc;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author Admin
*/
@Entity
@XmlRootElement
public class Book implements Serializable {
/**
* Get the value of Author
*
* @return the value of Author
*/
public String getAuthor() {
return Author;
}
/**
* Set the value of Author
*
* @param Author new value of Author
*/
public void setAuthor(String Author) {
this.Author = Author;
}
/**
* Get the value of Title
*
* @return the value of Title
*/
public String getTitle() {
return Title;
}
/**
* Set the value of Title
*
* @param Title new value of Title
*/
public void setTitle(String Title) {
this.Title = Title;
}
/**
* Get the value of Bname
*
* @return the value of Bname
*/
public String getBname() {
return Bname;
}
/**
* Set the value of Bname
*
* @param Bname new value of Bname
*/
public void setBname(String Bname) {
this.Bname = Bname;
}
/**
* Get the value of Price
*
* @return the value of Price
*/
public int getPrice() {
return Price;
}
/**
* Set the value of Price
*
* @param Price new value of Price
*/
public void setPrice(int Price) {
this.Price = Price;
}
/**
* Get the value of Quantity
*
* @return the value of Quantity
*/
public long getQuantity() {
return Quantity;
}
/**
* Set the value of Quantity
*
* @param Quantity new value of Quantity
*/
public void setQuantity(long Quantity) {
this.Quantity = Quantity;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Book)) {
return false;
}
Book other = (Book) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "abc.Book[ id=" + id + " ]";
}
BookController.java
package abc;
import abc.util.JsfUtil;
import abc.util.PaginationHelper;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
@Named("bookController")
@SessionScoped
public class BookController implements Serializable {
public BookController() {
}
@Override
public int getItemsCount() {
return getFacade().count();
}
@Override
public DataModel createPageDataModel() {
return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(),
getPageFirstItem() + getPageSize()}));
}
};
}
return pagination;
}
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("BookCreated
"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e,
ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("BookUpdated
"));
return "View";
} catch (Exception e) {
JsfUtil.addErrorMessage(e,
ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("BookDeleted
"));
} catch (Exception e) {
JsfUtil.addErrorMessage(e,
ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
@FacesConverter(forClass = Book.class)
public static class BookControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String
value) {
if (value == null || value.length() == 0) {
return null;
}
BookController controller = (BookController)
facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "bookController");
return controller.getBook(getKey(value));
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object
object) {
if (object == null) {
return null;
}
if (object instanceof Book) {
Book o = (Book) object;
return getStringKey(o.getId());
} else {
throw new IllegalArgumentException("object " + object + " is of type " +
object.getClass().getName() + "; expected type: " + Book.class.getName());
}
}
}
Conclusion
The Book class is a fundamental component of the Online Bookstore API, providing a structured
representation of book data. It leverages JPA for ORM (Object-Relational Mapping) and includes
essential methods for managing book attributes.
Strengths:
Future Directions:
Overall, the Book class provides a solid foundation for managing book data in an online bookstore. By
addressing the identified areas for improvement, the class can be enhanced to better support the needs of
the application and ensure data consistency and reliability.
(CRUD Operation)
Create:
View :
Update :
Delete :