0% found this document useful (0 votes)
20 views15 pages

9124 Rohan Gupta WS CA2

Uploaded by

amang22cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

9124 Rohan Gupta WS CA2

Uploaded by

amang22cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MAHATMA EDUCATION SOCIETY’S

PILLAI COLLEGE OF ARTS, COMMERCE & SCIENCE

(Autonomous)

NEW PANVEL

PROJECT REPORT ON

“OnlineBookstoreAPI”

IN PARTIAL FULFILLMENT OF

BACHELOR OF COMPUTER SCIENCE

SEMESTER V - 2024-25

PROJECT GUIDE

NAME: Abhijeet Salvi

SUBMITTED BY : Rohan Gupta

ROLL NO : 9124
Overview

Class Name: Book

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.

Includes an auto-generated unique identifier for each book via @GeneratedValue(strategy =


GenerationType.AUTO).

Attributes:

id: The primary key of the book, automatically generated.


Author: The author of the book.
Title: The title of the book.
Bname: The book’s name, which could be redundant with Title.
Price: The price of the book.
Quantity: The available quantity of the book.

Serialization and XML Binding:

Implements Serializable, supporting serialization and deserialization.

Annotated with @XmlRootElement, enabling XML representation for web services.

Getters and Setters:

Provides methods to access and modify the private fields, adhering to encapsulation principles.

Equality and Hashing:

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:

Implements Serializable, which is important for serialization and deserialization processes,


especially in distributed systems.
Automatic ID Generation:

Uses @GeneratedValue(strategy = GenerationType.AUTO) to automatically generate


unique IDs for each Book entity.
Getters and Setters:

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:

Inconsistent Field Naming:

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.

Deprecated or Unnecessary Methods:

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:

Validation and Constraints:

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.

Integration with Other Services:

Extend the class to support additional features like integration with external APIs for book information or
price updates.

Improved Field Naming:

Refactor the field names to follow common Java naming conventions, which will enhance code
readability and maintainability.

Threats:

Data Consistency Issues:

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 {

private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

public Long getId() {


return id;
}

private String Author;

/**
* 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;
}

private String Title;

/**
* 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;
}

private String Bname;

/**
* 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;
}

private int Price;

/**
* 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;
}

private long Quantity;

/**
* 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;
}

public void setId(Long id) {


this.id = id;
}

@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 {

private Book current;


private DataModel items = null;
@EJB
private abc.BookFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;

public BookController() {
}

public Book getSelected() {


if (current == null) {
current = new Book();
selectedItemIndex = -1;
}
return current;
}

private BookFacade getFacade() {


return ejbFacade;
}

public PaginationHelper getPagination() {


if (pagination == null) {
pagination = new PaginationHelper(10) {

@Override
public int getItemsCount() {
return getFacade().count();
}

@Override
public DataModel createPageDataModel() {
return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(),
getPageFirstItem() + getPageSize()}));
}
};
}
return pagination;
}

public String prepareList() {


recreateModel();
return "List";
}

public String prepareView() {


current = (Book) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "View";
}

public String prepareCreate() {


current = new Book();
selectedItemIndex = -1;
return "Create";
}

public String create() {


try {
getFacade().create(current);

JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("BookCreated
"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e,
ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}

public String prepareEdit() {


current = (Book) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "Edit";
}

public String update() {


try {
getFacade().edit(current);

JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("BookUpdated
"));
return "View";
} catch (Exception e) {
JsfUtil.addErrorMessage(e,
ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}

public String destroy() {


current = (Book) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
performDestroy();
recreatePagination();
recreateModel();
return "List";
}

public String destroyAndView() {


performDestroy();
recreateModel();
updateCurrentItem();
if (selectedItemIndex >= 0) {
return "View";
} else {
// all items were removed - go back to list
recreateModel();
return "List";
}
}
private void performDestroy() {
try {
getFacade().remove(current);

JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("BookDeleted
"));
} catch (Exception e) {
JsfUtil.addErrorMessage(e,
ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}

private void updateCurrentItem() {


int count = getFacade().count();
if (selectedItemIndex >= count) {
// selected index cannot be bigger than number of items:
selectedItemIndex = count - 1;
// go to previous page if last page disappeared:
if (pagination.getPageFirstItem() >= count) {
pagination.previousPage();
}
}
if (selectedItemIndex >= 0) {
current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex +
1}).get(0);
}
}

public DataModel getItems() {


if (items == null) {
items = getPagination().createPageDataModel();
}
return items;
}

private void recreateModel() {


items = null;
}

private void recreatePagination() {


pagination = null;
}

public String next() {


getPagination().nextPage();
recreateModel();
return "List";
}

public String previous() {


getPagination().previousPage();
recreateModel();
return "List";
}

public SelectItem[] getItemsAvailableSelectMany() {


return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
}

public SelectItem[] getItemsAvailableSelectOne() {


return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
}

public Book getBook(java.lang.Long id) {


return ejbFacade.find(id);
}

@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));
}

java.lang.Long getKey(String value) {


java.lang.Long key;
key = Long.valueOf(value);
return key;
}

String getStringKey(java.lang.Long value) {


StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}

@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:

• The class effectively uses JPA annotations to facilitate database interactions.


• It supports XML binding, making it compatible with web services.

Areas for Improvement:

• Field Naming: Standardize field names for consistency and clarity.


• Validation: Incorporate validation to ensure data integrity.
• Method Refinement: Improve the equals and hashCode methods to ensure reliable
functionality.
• Default Constructor: Ensure the presence of a no-argument constructor if not already included.

Future Directions:

• Implement field-level validation using annotations like @NotNull or @Min.


• Consider adding additional utility methods to enhance functionality.
• Refactor naming conventions for better alignment with Java best practices.

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 :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy