Skip to content

Commit

Permalink
add license
Browse files Browse the repository at this point in the history
  • Loading branch information
lutfialquraan committed May 9, 2019
1 parent b5d0136 commit 178c3a2
Show file tree
Hide file tree
Showing 19 changed files with 382 additions and 25 deletions.
23 changes: 23 additions & 0 deletions PATCHES/create-license-table
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
create table license
(
id int not null,
email varchar not null,
license varchar
);

create unique index license_id_uindex
on license (id);

alter table license_table
add constraint license_pk
primary key (id);

create sequence license_id_seq;

alter table license_table alter column id set default nextval('public.license_id_seq');

alter sequence license_id_seq owned by license.id;

alter table license_table alter column license type date using license::date;


Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package controller.actions.admin.users;

import at.favre.lib.crypto.bcrypt.BCrypt;
import controller.actions.IAction;
import model.database.DAO;
import model.database.LicenseDAO;
import model.database.UsersDAO;
import model.enums.Role;
import model.license.License;
import model.users.AbstractBaseUser;
import model.users.Admin;
import model.users.BasicUser;
import model.users.SuperAdmin;
import utilities.AccessControl;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

public class AddLicenseAction implements IAction {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {

Role role = (Role) request.getSession().getAttribute("role");
if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) {
String email = request.getParameter("email");
LocalDate date = LocalDate.now();
date = date.plusDays(Integer.parseInt(request.getParameter("date")));

License license = new License(email,date);

DAO licenseDAO = new LicenseDAO();
licenseDAO.insert(license);
response.sendRedirect("/admin/showLicense");
} else {
response.sendRedirect("/admin");
}
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {

Role role = (Role) request.getSession().getAttribute("role");
if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) {
RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/admin/license/add-license.jsp");
requestDispatcher.forward(request, response);
} else {
response.sendRedirect("/admin");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package controller.actions.admin.license;

import controller.actions.IAction;
import model.enums.Role;
import utilities.AccessControl;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShowHomeLicenseAction implements IAction {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
doGet(request,response);
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
Role role = (Role) request.getSession().getAttribute("role");
if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) {
RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/admin/license/admin-license-home.jsp");
requestDispatcher.forward(request, response);
} else {
response.sendRedirect("/admin");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package controller.actions.admin.license;

import controller.actions.IAction;
import model.contents.ContentMeta;
import model.database.ContentMetaDAO;
import model.database.DAO;
import model.database.LicenseDAO;
import model.enums.Role;
import model.license.License;
import utilities.AccessControl;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ShowLicensesAction implements IAction {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
doGet(request,response);
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
Role role = (Role) request.getSession().getAttribute("role");
if (AccessControl.isLoggedIn(request) && (AccessControl.isAdmin(role) || AccessControl.isSuperAdmin(role))) {
DAO licenseDAO = new LicenseDAO();
List<License> license = (List<License>) (List<?>) licenseDAO.selectAll();
request.setAttribute("license", license);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/admin/license/show-licenses.jsp");
requestDispatcher.forward(request, response);
} else {
response.sendRedirect("/admin");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller.actions.user;
package controller.actions.user.access;

import controller.actions.IAction;
import model.database.DAO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller.actions.user;
package controller.actions.user.access;

import controller.actions.IAction;
import utilities.ControlSession;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller.actions.user;
package controller.actions.user.access;

import at.favre.lib.crypto.bcrypt.BCrypt;
import controller.actions.IAction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller.actions.user;
package controller.actions.user.preview;

import controller.actions.IAction;
import utilities.AccessControl;
Expand All @@ -15,7 +15,7 @@ public class GetArticleAction implements IAction {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {

if (AccessControl.isLoggedIn(request)) {
if (AccessControl.isLoggedIn(request) && AccessControl.hasLicense(request) ) {
String doi = request.getParameter("doi");
String htmlPath = DirectoryPaths.CONTENTS_FILE_PATH + doi + ".html";
String pdf = DirectoryPaths.CONTENTS_FILE_PATH + doi + ".pdf";
Expand Down Expand Up @@ -44,4 +44,4 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
doPost(request, response);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller.actions.user;
package controller.actions.user.preview;

import controller.actions.IAction;
import model.contents.ContentMeta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller.actions.user;
package controller.actions.user.preview;

import controller.actions.IAction;

Expand Down
85 changes: 80 additions & 5 deletions src/main/java/model/database/LicenseDAO.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,105 @@
package model.database;

import model.license.License;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class LicenseDAO extends DAO {
@Override
public void insert(Object object) {

try {
License license = (License) object;
String sql = "insert into license_table (email,license) values (?,?)";
PreparedStatement preparedStatement = connectionPool.getConnection().prepareStatement(sql);
preparedStatement.setString(1, license.getEmail());
preparedStatement.setObject(2, license.getDate());
preparedStatement.execute();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void delete(Object object) {

try {
String email = (String) object;
String sql = "delete from license_table where email = ? ";
PreparedStatement preparedStatement = connectionPool.getConnection().prepareStatement(sql);
preparedStatement.setString(1, email);
preparedStatement.execute();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void update(Object object) {

try {
License license = (License) object;
String sql = "update license_table set license =? where id = ? ;";
PreparedStatement preparedStatement = connectionPool.getConnection().prepareStatement(sql);
preparedStatement.setObject(1,license.getDate());
preparedStatement.setInt(2, license.getId());
preparedStatement.execute();
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public List<Object> selectAll() {
return null;
List<Object> licenses = new ArrayList<>();
Statement myStat;
ResultSet myRes;
try {
String sql = "select * from license_table order by id";
myStat = connectionPool.getConnection().createStatement();
myRes = myStat.executeQuery(sql);
licenses = getLicense(myRes);
} catch (Exception e) {
e.printStackTrace();
}
return licenses;
}

@Override
public Object select(Object object) {
return null;
License license = null;
PreparedStatement myStat;
String email = (String) object;
ResultSet myRes = null;
try {
String sql = "select * from license_table where email = ?";
myStat = connectionPool.getConnection().prepareStatement(sql);
myStat.setString(1, email);
myRes = myStat.executeQuery();
license = (License) getLicense(myRes).get(0);
} catch (Exception e) {
e.printStackTrace();
}
return license;
}

private List<Object> getLicense(ResultSet resultSet) {
List<Object> licenses = new ArrayList<>();
License theLicense = null;
try {
while (resultSet.next()) {

String email = resultSet.getString("email");
LocalDate license = resultSet.getObject("license",LocalDate.class);
int id = resultSet.getInt("id");
theLicense = new License(id,email,license);
licenses.add(theLicense);
}
} catch (Exception e) {
e.printStackTrace();
}
return licenses;
}
}
2 changes: 1 addition & 1 deletion src/main/java/model/database/SubmittedFileDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Object select(Object object) {
int file_id = (int) object;
ResultSet myRes = null;
try {
String sql = "select * from user_table where file_id = ?";
String sql = "select * from unprocessed_file where file_id = ?";
myStat = connectionPool.getConnection().prepareStatement(sql);
myStat.setInt(1, file_id);
myRes = myStat.executeQuery();
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/model/license/License.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
package model.license;

import java.time.LocalDate;

public class License {
private int id;
private String email;
private LocalDate date;

public License(int id, String email, LocalDate date) {
this.id = id;
this.email = email;
this.date = date;
}

public License(String email, LocalDate date) {
this.email = email;
this.date = date;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public LocalDate getDate() {
return date;
}

public void setDate(LocalDate date) {
this.date = date;
}
}
Loading

0 comments on commit 178c3a2

Please sign in to comment.