Java 2

Week 14

UML Sequence Diagram

  • A sequence diagram describes the sequence in which objects and components interact with each other to complete a process.
  • A sequence diagram can be drawn to represent the details of a UML use case.
  • This diagram indicates that we want the Admin user to receive an email notification anytime the system detects a server error.
    • There are times that server errors occur that the developers are unaware of. This will help troubleshoot and debug it.

Mermaid

  • Mermaid is a JavaScript based diagramming and charting tool. You input text and it generates diagrams.
  • Click the "Live Editor" button and enter the following code.
    • The ->> arrows represent the direction of the message flow.

    • The -->> arrows indicate a self-message.

    • The labels on the arrows describe the actions or events occurring at each step in the sequence.

sequenceDiagram
    actor User
    actor AdminUser
    participant System
    participant AzureCommunication

    User ->> System: <br>Http Request 
    System -->> System: Detect 500 error
    System ->> AzureCommunication: NotifyAdminByEmail
    AzureCommunication -->> AzureCommunication: Prepare<br>Email Notification
    AzureCommunication ->> AdminUser: Email Notification

Draw.io

Final Exam Review

Intellectual Property

Copyright

  • Copyright is a form of legal protection given to content creators of original expression.
  • Copyright protection exists automatically from the time a qualifying work (such as a software program) is fixed in a tangible medium.
  • Only the expression, and not the idea, is protected by copyright law.
  • Neither publication, registration, nor other action is required to secure a copyright.
  • Registering your copyright only becomes necessary when you need take your copyright-related dispute to court.
  • In most cases and countries today, the general rule is that copyright lasts for the life of the author and then until 31 December of the year 70 years after his or her death (usually referred to as “life plus 70”).

Copyright

  • A company's copyright lasts 120 years from creation or 95 years from publication, whichever is shorter.
    • For example, Disney lost the Mickey Mouse copyright for Steamboat Willie in 2024, since the short animated film was produced and distributed in 1928.
    • Any future iterations of Mickey Mouse – including any showing Mickey Mouse in color – are still under Disney's control.
  • The public domain refers to works that are no longer protected by copyright. Either the copyright has expired, or the author applied a license granting immediate release.
  • A DMCA takedown is a request to remove copyrighted from a website or other platform at the request of the owner of the copyright. Before sending a DMCA takedown notice try contacting the offending site or user directly with a nice, polite email.

Open Source Licenses

  • MIT (See tailwindcss or dotnet)
    The MIT License is a permissive software license that allows users to do whatever they want with the software, including using it in commercial products.
  • GNU General Public License (GPL)
    The GNU GPL is a copyleft software license that requires users to distribute the software under the same license as they received it.
  • The MIT License is more permissive and allows for more freedom, while the GNU GPL is more restrictive and requires users to share the software with others under the same license. If you want to give users as much freedom as possible, then the MIT License is a good choice.

Commercial Licenses

Fair Use

  • Fair use occurs when a person or organization is permitted to use copyrighted work without the authorization of the copyright holder.
  • Fair use is ultimately nothing more than a legal defense, not something you sign up or get permission in advance for.
  • Most countries in the world do recognize exceptions to copyright law for educational purposes or when its use serves a sufficiently important public goal.
  • Companies like Google, the New York Times, and Kirkwood Community College rely on fair use to be able to do what they do. Its use can be for profit or non-profit.
  • Fair Use Article

Stored Procedure

  • Create a stored procedure to update a vendor.
  • The orig_ prefix identifies parameters for the original vendor, and new_ identifies parameters for the new vendor.
  • Conditional Update: The WHERE clause includes conditions to ensure the update only happens if all original vendor details match the current data in the vendors table.
    • This approach ensures that updates are performed only if the data hasn't been modified by another process, preserving data integrity.
CREATE PROCEDURE sp_update_vendor_admin(
    IN orig_vend_id      VARCHAR(10),
    IN orig_vend_name    VARCHAR(50),
    IN orig_vend_address VARCHAR(50),
    IN orig_vend_city    VARCHAR(50),
    IN orig_vend_state   VARCHAR(5),
    IN orig_vend_zip     VARCHAR(10),
    IN orig_vend_country VARCHAR(50),
    IN new_vend_id       VARCHAR(10),
    IN new_vend_name     VARCHAR(50),
    IN new_vend_address  VARCHAR(50),
    IN new_vend_city     VARCHAR(50),
    IN new_vend_state    VARCHAR(5),
    IN new_vend_zip      VARCHAR(10),
    IN new_vend_country  VARCHAR(50)
)
BEGIN
    UPDATE vendors
    SET vend_name = new_vend_name,
        vend_address = new_vend_address,
        vend_city = new_vend_city,
        vend_state = new_vend_state,
        vend_zip = new_vend_zip,
        vend_country = new_vend_country
    WHERE vend_id = orig_vend_id
      AND vend_name = orig_vend_name
      AND vend_address = orig_vend_address
      AND vend_city = orig_vend_city
      AND vend_state = orig_vend_state
      AND vend_zip = orig_vend_zip
      AND vend_country = orig_vend_country;
END;

VendorDAO

  • Copy the addVendor method. Change it to update a Vendor.
public static boolean updateVendor(Vendor vendor) {
    try(Connection connection = getConnection()) {
        CallableStatement statement = connection.prepareCall("{CALL sp_update_vendor_admin(?, ?, ?, ?, ?, ?, ?)}");
        statement.setString(1, vendor.getVend_id());
        statement.setString(2, vendor.getVend_name());
        statement.setString(3, vendor.getAddress().getAddress());
        statement.setString(4, vendor.getAddress().getCity());
        statement.setString(5, vendor.getAddress().getState());
        statement.setString(6, vendor.getAddress().getZip());
        statement.setString(7, vendor.getAddress().getCountry());
        int rowsAffected = statement.executeUpdate();
        return rowsAffected == 1;
    } catch(SQLException e) {
//            System.out.println(e.getMessage()); // Uncomment in case nothing is inserting
        return false;
    }
}

AdminUpdateVendor

  • In your "admin-vendors.jsp" file, find your Edit/Delete links. They likely look like this:
  • <a href="edit-vendor?vend_id=${vendor.vend_id}"
  • Create an AdminUpdateVendor servlet. Use a URL value equal to the red part of the hyperlink.
  • Add a doGet method that gets the parameter equal to the green part of the hyperlink (this is called a query string parameter).
  • Use that parameter to call the getVendor method from the DAO.
import edu.kirkwood.ecommerce.model.Vendor;
import edu.kirkwood.ecommerce.model.VendorDAO;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(value="/edit-vendor")
public class AdminUpdateVendor extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String vend_id = req.getParameter("vend_id");
        Vendor vendor = VendorDAO.getVendor(vend_id);
        req.setAttribute("vendor", vendor);
        req.getRequestDispatcher("WEB-INF/ecommerce/admin-update-vendor.jsp").forward(req, resp);
    }
}

admin-update-vendor.jsp

  • Create a "admin-update-vendor.jsp" file.
  • Add a title, main-nav, container, button to view all vendors, heading, and c:choose tag to display something if no vendor is found.
  • Run the server, click an Edit Vendor link. Change the vend_id parameter to something that doesn't exist to display the message
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin - Update Vendor</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<%@ include file="../../main-nav.jsp"%>
<div class="container py-4">
    <a href="vendors" class="btn btn-primary mb-4" role="button">View All Vendors</a>
    <h2>Admin - Update Vendor</h2>
    <c:choose>
        <c:when test="${empty vendor}">
            <p class="lead">No vendor found</p>
        </c:when>
        <c:otherwise>
            
        </c:otherwise>
    </c:choose>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
        crossorigin="anonymous"></script>
</body>
</html>

Bootstrap Form

  • Open admin-add-vendor.jsp. Copy the <c:if> tag and form tag. Paste it inside the c:otherwise tag of admin-update-vendor.jsp
  • Change the value attributes to reflect the Vendor attribute's getter methods.
  • Run the server, click an Edit Vendor link. The vendor's data should display in the form.
<c:if test="${not empty vendorAdded}">
    <div class="alert <c:choose><c:when test="${vendorAdded == true}">alert-success</c:when><c:otherwise>alert-danger</c:otherwise></c:choose>" role="alert">
            ${vendorAddedMessage}
    </div>
</c:if>
<form class="row g-3" method="POST" action="add-vendor">
    <div class="col-md-3">
        <label for="vendorId" class="form-label">Vendor Id</label>
        <input type="text" class="form-control <c:choose><c:when test="${vendorIdError == true}">is-invalid</c:when><c:when test="${vendorIdError == false}">is-valid</c:when><c:otherwise></c:otherwise></c:choose>" id="vendorId" name="vendorId" value="${vendor.vend_id}">
        <div class="<c:choose><c:when test="${vendorIdError == true}">invalid-feedback</c:when><c:when test="${vendorIdError == false}">valid-feedback</c:when><c:otherwise></c:otherwise></c:choose>">
                ${vendorIdMessage}
        </div>
    </div>
    <div class="col-md-9">
        <label for="vendorName" class="form-label">Vendor name</label>
        <input type="text" class="form-control <c:choose><c:when test="${vendorNameError == true}">is-invalid</c:when><c:when test="${vendorNameError == false}">is-valid</c:when><c:otherwise></c:otherwise></c:choose>" id="vendorName" name="vendorName" value="${vendor.vend_name}">
        <div class="<c:choose><c:when test="${vendorNameError == true}">invalid-feedback</c:when><c:when test="${vendorNameError == false}">valid-feedback</c:when><c:otherwise></c:otherwise></c:choose>">
                ${vendorNameMessage}
        </div>
    </div>

    <div class="col-md-4">
        <label for="country" class="form-label">Country Abbreviation</label>
        <input type="text" class="form-control <c:choose><c:when test="${countryError == true}">is-invalid</c:when><c:when test="${countryError == false}">is-valid</c:when><c:otherwise></c:otherwise></c:choose>" id="country" name="country" value="${vendor.address.country}" maxlength="3">
        <div class="<c:choose><c:when test="${countryError == true}">invalid-feedback</c:when><c:when test="${countryError == false}">valid-feedback</c:when><c:otherwise></c:otherwise></c:choose>">
                ${countryMessage}
        </div>
    </div>
    <div class="col-md-8">
        <label for="streetAddress" class="form-label">Street Address</label>
        <input type="text" class="form-control <c:choose><c:when test="${streetAddressError == true}">is-invalid</c:when><c:when test="${streetAddressError == false}">is-valid</c:when><c:otherwise></c:otherwise></c:choose>" id="streetAddress" name="streetAddress" value="${vendor.address.address}">
        <div class="<c:choose><c:when test="${streetAddressError == true}">invalid-feedback</c:when><c:when test="${streetAddressError == false}">valid-feedback</c:when><c:otherwise></c:otherwise></c:choose>">
                ${streetAddressMessage}
        </div>
    </div>

    <div class="col-md-4">
        <label for="city" class="form-label">City</label>
        <input type="text" class="form-control <c:choose><c:when test="${cityError == true}">is-invalid</c:when><c:when test="${cityError == false}">is-valid</c:when><c:otherwise></c:otherwise></c:choose>" id="city" name="city" value="${vendor.address.city}">
        <div class="<c:choose><c:when test="${cityError == true}">invalid-feedback</c:when><c:when test="${cityError == false}">valid-feedback</c:when><c:otherwise></c:otherwise></c:choose>">
                ${cityMessage}
        </div>
    </div>
    <div class="col-md-4">
        <label for="state" class="form-label">State Abbreviation</label>
        <input type="text" class="form-control <c:choose><c:when test="${stateError == true}">is-invalid</c:when><c:when test="${stateError == false}">is-valid</c:when><c:otherwise></c:otherwise></c:choose>" id="state" name="state" value="${vendor.address.state}" maxlength="2">
        <div class="<c:choose><c:when test="${stateError == true}">invalid-feedback</c:when><c:when test="${stateError == false}">valid-feedback</c:when><c:otherwise></c:otherwise></c:choose>">
                ${stateMessage}
        </div>
    </div>
    <div class="col-md-4">
        <label for="zip" class="form-label">Zip</label>
        <input type="text" class="form-control <c:choose><c:when test="${zipError == true}">is-invalid</c:when><c:when test="${zipError == false}">is-valid</c:when><c:otherwise></c:otherwise></c:choose>" id="zip" name="zip" value="${vendor.address.zip}">
        <div class="<c:choose><c:when test="${zipError == true}">invalid-feedback</c:when><c:when test="${zipError == false}">valid-feedback</c:when><c:otherwise></c:otherwise></c:choose>">
                ${zipMessage}
        </div>
    </div>

    <div class="col-12">
        <button class="btn btn-dark" type="submit">Submit form</button>
    </div>
</form>

Stored Procedure

  • Create a stored procedure to delete a vendor.
CREATE PROCEDURE sp_delete_product(IN p_vend_id varchar(10))
BEGIN
    DELETE FROM vendors WHERE vend_id = p_vend_id;
END;