Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 47.74 KB | None | 0 0
  1. import com.google.gson.Gson;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6. class Database {
  7.  
  8.     private Connection con;
  9.  
  10.     private static final int U_ADMIN = 0;
  11.     private static final int U_LIBRARIAN = 1;
  12.     private static final int U_PUBL_HOUSE = 2;
  13.     private static final int U_AUTHOR = 3;
  14.     private static final int U_READER = 4;
  15.  
  16.     private static final int S_WOMAN = 0;
  17.     private static final int S_MAN = 1;
  18.     private static final int S_OTHER = 2;
  19.  
  20.     private static final int P_BOOK = 0;
  21.     private static final int P_DIGEST = 1;
  22.     private static final int P_ARTICLE = 2;
  23.     private static final int P_THESES = 3;
  24.     private static final int P_DOCS = 4;
  25.  
  26.     Database() throws Exception {
  27.         Class.forName("org.postgresql.Driver");
  28.         con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/library_db", "victor", "password1");
  29.     }
  30.  
  31.     String authorizeUser(String login, String password) {
  32.         String s =
  33.                 "SELECT users_id, name, surname, patronymic, sex, login, birth_date, type, phone_number, email " +
  34.                 "FROM users WHERE login = ? AND password = ?";
  35.         try {
  36.             PreparedStatement ps = con.prepareStatement(s);
  37.             ps.setString(1, login);
  38.             ps.setString(2, password);
  39.             ResultSet rs = ps.executeQuery();
  40.             int columnCount = rs.getMetaData().getColumnCount();
  41.             String[] user = {"-1", "", "", "", "", "", "", "", "", ""};
  42.             if (rs.next()) {
  43.                 int i = 1;
  44.                 while (i <= columnCount) {
  45.                     user[i - 1] = rs.getString(i);
  46.                     ++i;
  47.                 }
  48.             }
  49.             return new Gson().toJson(user);
  50.  
  51.         } catch (SQLException e) {
  52.             return "error: " + e.getMessage();
  53.         }
  54.     }
  55.  
  56.     String getPublsOfUser(String caller_id) {
  57.         try {
  58.             String query =
  59.                     "SELECT p.publications_id " +
  60.                     "FROM publications p " +
  61.                     "LEFT JOIN authors_publications ap ON p.publications_id = ap.publications_id " +
  62.                     "LEFT JOIN users_authors ua ON ua.authors_id = ap.authors_id " +
  63.                     "LEFT JOIN publishing_houses_publications php ON p.publications_id = php.publications_id " +
  64.                     "LEFT JOIN users_publishing_houses uph ON uph.publishing_houses_id = php.publishing_houses_id " +
  65.                     "WHERE ua.users_id = ? OR uph.users_id = ?";
  66.             PreparedStatement ps = con.prepareStatement(query);
  67.             ps.setLong(1, Long.parseLong(caller_id));
  68.             ps.setLong(2, Long.parseLong(caller_id));
  69.             ResultSet rs = ps.executeQuery();
  70.             return RSToString(rs);
  71.         } catch (SQLException e) {
  72.             return "error: " + e.getMessage();
  73.         }
  74.     }
  75.  
  76.     String getUsers(String caller_id) {
  77.         if (!checkUserType(caller_id, new int[]{U_ADMIN})) {
  78.             return "error: access";
  79.         }
  80.         return execSelectQuery(
  81.                 "SELECT u.users_id, u.surname, u.name, u.patronymic, u.sex, u.login, " +
  82.                 "u.birth_date, u.type, u.phone_number, u.email " +
  83.                 "FROM users u");
  84.     }
  85.  
  86.     String getPublications() {
  87.         return execSelectQuery("SELECT * FROM publications");
  88.     }
  89.  
  90.     String getBooks() {
  91.         return execSelectQuery(
  92.                 "SELECT p.publications_id, p.title, ph.title, py.year " +
  93.                 "FROM publications p " +
  94.                 "JOIN publishing_houses_publications php on p.publications_id = php.publications_id " +
  95.                 "JOIN publishing_houses ph ON php.publishing_houses_id = ph.publishing_houses_id " +
  96.                 "JOIN publishing_years py on p.publications_id = py.publications_id " +
  97.                 "WHERE p.type = " + P_BOOK
  98.         );
  99.     }
  100.  
  101.     String getDigests() {
  102.         return execSelectQuery(
  103.                 "SELECT p.publications_id, p.title, ph.title, py.year " +
  104.                 "FROM publications p " +
  105.                 "JOIN publishing_houses_publications php on p.publications_id = php.publications_id " +
  106.                 "JOIN publishing_houses ph ON php.publishing_houses_id = ph.publishing_houses_id " +
  107.                 "JOIN publishing_years py on p.publications_id = py.publications_id " +
  108.                 "WHERE p.type = " + P_DIGEST
  109.         );
  110.     }
  111.  
  112.     String getArticles() {
  113.         return execSelectQuery(
  114.                 "SELECT p.publications_id, p.title, m.title, p1.title, mai.volume, mai.release_number " +
  115.                 "FROM publications p " +
  116.                 "LEFT JOIN digests_publications dp ON p.publications_id = dp.publications_id " +
  117.                 "LEFT JOIN magazines_publications mp ON p.publications_id = mp.publications_id " +
  118.                 "LEFT JOIN publications p1 ON p1.publications_id = dp.publications_id " +
  119.                 "LEFT JOIN magazines m on mp.magazines_id = m.magazines_id " +
  120.                 "LEFT JOIN magazine_article_info mai on p.publications_id = mai.publications_id " +
  121.                 "WHERE p.type = " + P_ARTICLE
  122.         );
  123.     }
  124.  
  125.     String getTheses() {
  126.         return execSelectQuery(
  127.                 "SELECT p.publications_id, p.title, p1.title, m.title " +
  128.                 "FROM publications p " +
  129.                 "LEFT JOIN digests_publications dp ON p.publications_id = dp.publications_id " +
  130.                 "LEFT JOIN magazines_publications mp ON p.publications_id = mp.publications_id " +
  131.                 "LEFT JOIN publications p1 ON p1.publications_id = dp.publications_id " +
  132.                 "LEFT JOIN magazines m on mp.magazines_id = m.magazines_id " +
  133.                 "WHERE p.type = " + P_THESES
  134.         );
  135.     }
  136.  
  137.     String getDocs() {
  138.         return execSelectQuery(
  139.                 "SELECT p.publications_id, p.title, o.title " +
  140.                 "FROM publications p " +
  141.                 "JOIN organizations_publications op on p.publications_id = op.publications_id " +
  142.                 "JOIN organizations o on op.organizations_id = o.organizations_id " +
  143.                 "WHERE p.type = " + P_DOCS
  144.         );
  145.     }
  146.  
  147.     String getSubjects() {
  148.         return execSelectQuery(
  149.                 "SELECT * FROM subjects"
  150.         );
  151.     }
  152.  
  153.     String getMagazines() {
  154.         return execSelectQuery(
  155.                 "SELECT m.magazines_id, m.title, s.title " +
  156.                 "FROM magazines m " +
  157.                 "JOIN subjects s on m.subjects_id = s.subjects_id"
  158.         );
  159.     }
  160.  
  161.     String getAuthors() {
  162.         return execSelectQuery(
  163.                 "SELECT * FROM authors"
  164.         );
  165.     }
  166.  
  167.     String getEditors() {
  168.         return execSelectQuery(
  169.                 "SELECT * FROM editors"
  170.         );
  171.     }
  172.  
  173.     String getOrganizations() {
  174.         return execSelectQuery(
  175.                 "SELECT * FROM organizations"
  176.         );
  177.     }
  178.  
  179.     String getPublHouses() {
  180.         return execSelectQuery(
  181.                 "SELECT * FROM publishing_houses"
  182.         );
  183.     }
  184.  
  185.     String getKeywords() {
  186.         return execSelectQuery(
  187.                 "SELECT * FROM keywords"
  188.         );
  189.     }
  190.  
  191.     String getUdc() {
  192.         return execSelectQuery(
  193.                 "SELECT * FROM udc_codes"
  194.         );
  195.     }
  196.  
  197.     String getVerfs(String caller_id) {
  198.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  199.             return "error: access";
  200.         }
  201.         return execSelectQuery(
  202.                 "SELECT v.verifications_id, v.users_id, u.login, u.phone_number, u.email, v.to_type, v.authors_id, " +
  203.                 "v.publishing_houses_id " +
  204.                 "FROM verifications v " +
  205.                 "JOIN users u on v.users_id = u.users_id"
  206.         );
  207.     }
  208.  
  209.     String getAuthorsWorktimes(String a_id) {
  210.         String query =
  211.                 "SELECT ao.authors_organizations_id, o.title, ao.start, ao.finish " +
  212.                 "FROM authors_organizations ao " +
  213.                 "JOIN authors a on ao.authors_id = ? " +
  214.                 "JOIN organizations o on ao.organizations_id = o.organizations_id";
  215.         return execPSWithId(a_id, query);
  216.     }
  217.  
  218.     String getAuthorsOfPubl(String p_id) {
  219.         String query =
  220.                 "SELECT a.authors_id, a.name, a.surname, a.patronymic, a.sex, a.birth_date, " +
  221.                 "a.death_date, a.phone_number, a.email " +
  222.                 "FROM authors a " +
  223.                 "JOIN authors_publications ap ON a.authors_id = ap.authors_id" +
  224.                 "AND ap.publications_id = ?";
  225.         return execPSWithId(p_id, query);
  226.     }
  227.  
  228.     String getEditorsOfPubl(String p_id) {
  229.         String query =
  230.                 "SELECT e.editors_id, e.name, e.surname, e.patronymic, e.sex, e.birth_date, " +
  231.                 "e.death_date, e.phone_number, e.email " +
  232.                 "FROM editors e " +
  233.                 "JOIN editors_publications ep ON e.editors_id = ep.editors_id " +
  234.                 "AND ep.publications_id = ?";
  235.         return execPSWithId(p_id, query);
  236.     }
  237.  
  238.     String getKeywordsOfPubl(String id) {
  239.         String s =
  240.                 "SELECT k.keywords_id, k.keyword " +
  241.                 "FROM keywords k " +
  242.                 "JOIN publications_keywords pk on k.keywords_id = pk.keywords_id " +
  243.                 "JOIN publications p on pk.publications_id = p.publications_id " +
  244.                 "WHERE p.publications_id = ?";
  245.         return execPSWithId(id, s);
  246.     }
  247.  
  248.     String getUdcOfPubl(String id) {
  249.         String s =
  250.                 "SELECT uc.udc_codes_id, uc.udc_code " +
  251.                 "FROM udc_codes uc " +
  252.                 "JOIN publications_udc_codes puc ON uc.udc_codes_id = puc.udc_codes_id " +
  253.                 "JOIN publications p on puc.publications_id = p.publications_id " +
  254.                 "WHERE p.publications_id = ?";
  255.         return execPSWithId(id, s);
  256.     }
  257.  
  258.     String addUser(String caller_id, String name, String surname, String patronymic, String sex, String login,
  259.                    String password, String birth_date, String type, String phone_number, String email) {
  260.         if (!checkUserType(caller_id, new int[]{U_ADMIN})) {
  261.             return "error: access";
  262.         }
  263.         try {
  264.             String query =
  265.                     "INSERT INTO users(name, surname, patronymic, sex, login, password, birth_date, type, " +
  266.                     "phone_number, email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  267.             PreparedStatement ps = con.prepareStatement(query);
  268.             setFullNameAndSexInPs(ps, name, surname, patronymic, sex);
  269.             ps.setString(5, login);
  270.             ps.setString(6, password);
  271.             setDateInPs(ps, birth_date, 7);
  272.             ps.setInt(8, Integer.parseInt(type));
  273.             setPhoneAndEmailInPs(ps, phone_number, email, 9);
  274.             ps.executeUpdate();
  275.             return "ok";
  276.         } catch (Exception e) {
  277.             return "error: " + e.getMessage();
  278.         }
  279.     }
  280.  
  281.     String addBook(String caller_id, String title, String ph_id, String year) {
  282.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  283.             return "error: access";
  284.         }
  285.         return addBookAndDigestCommonPart(title, ph_id, year, P_BOOK);
  286.     }
  287.  
  288.     String addDigest(String caller_id, String title, String ph_id, String year) {
  289.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  290.             return "error: access";
  291.         }
  292.         return addBookAndDigestCommonPart(title, ph_id, year, P_DIGEST);
  293.     }
  294.  
  295.     private String addBookAndDigestCommonPart(String title, String ph_id, String year, int type) {
  296.         try {
  297.             String query_p =
  298.                     "INSERT INTO publications(type, title) VALUES (" + type + ", ?) " +
  299.                     "RETURNING publications_id";
  300.             addPublHouseAndYear(title, ph_id, year, query_p);
  301.             return "ok";
  302.         } catch (Exception e) {
  303.             return "error: " + e.getMessage();
  304.         }
  305.     }
  306.  
  307.     String addMagazineArticle(String caller_id, String title, String m_id, String volume, String relase_number) {
  308.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  309.             return "error: access";
  310.         }
  311.         try {
  312.             String query_p =
  313.                     "INSERT INTO publications(type, title) VALUES (" + P_ARTICLE + ", ?) " +
  314.                     "RETURNING publications_id";
  315.             long p_id = getPublIdAfterInsert(title, query_p);
  316.  
  317.             addMagazinePublication(p_id, m_id);
  318.  
  319.             String query_mai =
  320.                     "INSERT INTO magazine_article_info(publications_id, volume, release_number) " +
  321.                     "VALUES (?, ?, ?)";
  322.             PreparedStatement ps_mai = con.prepareStatement(query_mai);
  323.             ps_mai.setLong(1, p_id);
  324.             ps_mai.setLong(2, Integer.parseInt(volume));
  325.             ps_mai.setLong(3, Integer.parseInt(relase_number));
  326.             ps_mai.executeUpdate();
  327.             return "ok";
  328.         } catch (Exception e) {
  329.             return "error: " + e.getMessage();
  330.         }
  331.     }
  332.  
  333.     String addDigestArticle(String caller_id, String title, String d_id) {
  334.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  335.             return "error: access";
  336.         }
  337.         return addPublicationWithDigest(P_ARTICLE, title, d_id);
  338.     }
  339.  
  340.     String addMagazineTheses(String caller_id, String title, String m_id) {
  341.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  342.             return "error: access";
  343.         }
  344.         try {
  345.             String query_p =
  346.                     "INSERT INTO publications(type, title) VALUES (" + P_THESES + ", ?) " +
  347.                     "RETURNING publications_id";
  348.             long p_id = getPublIdAfterInsert(title, query_p);
  349.             addMagazinePublication(p_id, m_id);
  350.             return "ok";
  351.         } catch (Exception e) {
  352.             return "error: " + e.getMessage();
  353.         }
  354.     }
  355.  
  356.     String addDigestTheses(String caller_id, String title, String d_id) {
  357.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  358.             return "error: access";
  359.         }
  360.         return addPublicationWithDigest(P_THESES, title, d_id);
  361.     }
  362.  
  363.     String addDocs(String caller_id, String title, String o_id) {
  364.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  365.             return "error: access";
  366.         }
  367.         try {
  368.             String query_p =
  369.                     "INSERT INTO publications(type, title) VALUES (" + P_DOCS + ", ?) " +
  370.                     "RETURNING publications_id";
  371.             long p_id = getPublIdAfterInsert(title, query_p);
  372.             String query_op = "INSERT INTO organizations_publications(publications_id, organizations_id) VALUES (?, ?)";
  373.             PreparedStatement ps_op = con.prepareStatement(query_op);
  374.             ps_op.setLong(1, p_id);
  375.             ps_op.setInt(2, Integer.parseInt(o_id));
  376.             ps_op.executeUpdate();
  377.             return "ok";
  378.         } catch (Exception e) {
  379.             return "error: " + e.getMessage();
  380.         }
  381.     }
  382.  
  383.     private String addPublicationWithDigest(int type, String title, String d_id) {
  384.         try {
  385.             String query_p =
  386.                     "INSERT INTO publications(type, title) VALUES (" + type + ", ?) " + "RETURNING publications_id";
  387.             long p_id = getPublIdAfterInsert(title, query_p);
  388.             addDigestPublication(p_id, d_id);
  389.             return "ok";
  390.         } catch (Exception e) {
  391.             return "error: " + e.getMessage();
  392.         }
  393.     }
  394.  
  395.     private void addMagazinePublication(long p_id, String m_id) throws Exception {
  396.         String query_mp = "INSERT INTO magazines_publications(publications_id, magazines_id) VALUES (?, ?)";
  397.         PreparedStatement ps_mp = con.prepareStatement(query_mp);
  398.         ps_mp.setLong(1, p_id);
  399.         ps_mp.setInt(2, Integer.parseInt(m_id));
  400.         ps_mp.executeUpdate();
  401.     }
  402.  
  403.     private void addDigestPublication(long p_id, String d_id) throws Exception {
  404.         String query_dp = "INSERT INTO digests_publications(publications_id, digests_id) VALUES (?, ?)";
  405.         PreparedStatement ps_dp = con.prepareStatement(query_dp);
  406.         ps_dp.setLong(1, p_id);
  407.         ps_dp.setInt(2, Integer.parseInt(d_id));
  408.         ps_dp.executeUpdate();
  409.     }
  410.  
  411.     private long getPublIdAfterInsert(String title, String query_p) throws Exception {
  412.         PreparedStatement ps_p = con.prepareStatement(query_p, PreparedStatement.RETURN_GENERATED_KEYS);
  413.         ps_p.setString(1, title);
  414.         ps_p.executeUpdate();
  415.         ResultSet rs = ps_p.getGeneratedKeys();
  416.         rs.next();
  417.         return rs.getLong(1);
  418.     }
  419.  
  420.     private void addPublHouseAndYear(String title, String ph_id, String year, String query_p) throws Exception {
  421.         long p_id = getPublIdAfterInsert(title, query_p);
  422.  
  423.         String query_php =
  424.                 "INSERT INTO publishing_houses_publications(publications_id, publishing_houses_id) " +
  425.                 "VALUES (?, ?)";
  426.         PreparedStatement ps_php = con.prepareStatement(query_php);
  427.         ps_php.setLong(1, p_id);
  428.         ps_php.setInt(2, Integer.parseInt(ph_id));
  429.         ps_php.executeUpdate();
  430.  
  431.         String query_pe = "INSERT INTO publishing_years(publications_id, year) VALUES (?, ?)";
  432.         PreparedStatement ps_pe = con.prepareStatement(query_pe);
  433.         ps_pe.setLong(1, p_id);
  434.         ps_pe.setInt(2, Integer.parseInt(year));
  435.         ps_pe.executeUpdate();
  436.     }
  437.  
  438.     String addSubject(String caller_id, String subject) {
  439.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  440.             return "error: access";
  441.         }
  442.         try {
  443.             String query = "INSERT INTO subjects(title) VALUES (?)";
  444.             PreparedStatement ps = con.prepareStatement(query);
  445.             ps.setString(1, subject);
  446.             ps.executeUpdate();
  447.             return "ok";
  448.         } catch (Exception e) {
  449.             return "error: " + e.getMessage();
  450.         }
  451.     }
  452.  
  453.     String addMagazine(String caller_id, String title, String s_id, String o_id) {
  454.         if (!checkUserType(
  455.                 caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  456.             return "error: access";
  457.         }
  458.         try {
  459.             String query = "INSERT INTO magazines(title, subjects_id, organizations_id) VALUES (?, ?, ?)";
  460.             PreparedStatement ps = con.prepareStatement(query);
  461.             ps.setString(1, title);
  462.             ps.setLong(2, Long.parseLong(s_id));
  463.             ps.setLong(3, Long.parseLong(o_id));
  464.             ps.executeUpdate();
  465.             return "ok";
  466.         } catch (Exception e) {
  467.             return "error: " + e.getMessage();
  468.         }
  469.     }
  470.  
  471.     String addAuthor(String caller_id, String name, String surname, String patronymic, String sex, String birth_date,
  472.                      String death_date, String phone_number, String email) {
  473.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  474.             return "error: access";
  475.         }
  476.         String query =
  477.                 "INSERT INTO authors(name, surname, patronymic, sex, birth_date, death_date, phone_number, email) " +
  478.                 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
  479.         return addAuthorAndEditorCommonPart(query, name, surname, patronymic, sex, birth_date, death_date, phone_number,
  480.                 email);
  481.     }
  482.  
  483.     String addEditor(String caller_id, String name, String surname, String patronymic, String sex, String birth_date,
  484.                      String death_date, String phone_number, String email) {
  485.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  486.             return "error: access";
  487.         }
  488.         String query =
  489.                 "INSERT INTO editors(name, surname, patronymic, sex, birth_date, death_date, phone_number, email) " +
  490.                 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
  491.         return addAuthorAndEditorCommonPart(query, name, surname, patronymic, sex, birth_date, death_date, phone_number,
  492.                 email);
  493.     }
  494.  
  495.     String addAuthorAndEditorCommonPart(String query, String name, String surname, String patronymic, String sex,
  496.                                         String birth_date, String death_date, String phone_number, String email) {
  497.         try {
  498.             PreparedStatement ps = con.prepareStatement(query);
  499.             setAuthOrEditorInfoInPS(ps, name, surname, patronymic, sex, birth_date, death_date, phone_number, email);
  500.             ps.executeUpdate();
  501.             return "ok";
  502.         } catch (Exception e) {
  503.             return "error: " + e.getMessage();
  504.         }
  505.     }
  506.  
  507.     String addOrganization(String caller_id, String title, String address, String phone, String email) {
  508.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  509.             return "error: access";
  510.         }
  511.         String query = "INSERT INTO organizations(title, legal_address, phone_number, email) VALUES (?, ?, ?, ?)";
  512.         return addOrgAndPublHouseCommonPart(query, title, address, phone, email);
  513.     }
  514.  
  515.     String addPublHouse(String caller_id, String title, String address, String phone, String email) {
  516.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  517.             return "error: access";
  518.         }
  519.         String query = "INSERT INTO publishing_houses(title, legal_address, phone_number, email) VALUES (?, ?, ?, ?)";
  520.         return addOrgAndPublHouseCommonPart(query, title, address, phone, email);
  521.     }
  522.  
  523.     private String addOrgAndPublHouseCommonPart(String query, String title, String address, String phone,
  524.                                                 String email) {
  525.         try {
  526.             PreparedStatement ps = con.prepareStatement(query);
  527.             setOrgOrPublHouseInfoInPS(ps, title, address, phone, email);
  528.             ps.executeUpdate();
  529.             return "ok";
  530.         } catch (Exception e) {
  531.             return "error: " + e.getMessage();
  532.         }
  533.     }
  534.  
  535.     String addKeyword(String caller_id, String keyword) {
  536.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  537.             return "error: access";
  538.         }
  539.         try {
  540.             String query = "INSERT INTO keywords(keyword) VALUES (?)";
  541.             PreparedStatement ps = con.prepareStatement(query);
  542.             ps.setString(1, keyword);
  543.             ps.executeUpdate();
  544.             return "ok";
  545.         } catch (Exception e) {
  546.             return "error: " + e.getMessage();
  547.         }
  548.     }
  549.  
  550.     String addUdc(String caller_id, String udc) {
  551.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  552.             return "error: access";
  553.         }
  554.         try {
  555.             String query = "INSERT INTO udc_codes(udc_code) VALUES (?)";
  556.             PreparedStatement ps = con.prepareStatement(query);
  557.             ps.setString(1, udc);
  558.             ps.executeUpdate();
  559.             return "ok";
  560.         } catch (Exception e) {
  561.             return "error: " + e.getMessage();
  562.         }
  563.     }
  564.  
  565.     String addVerf(String caller_id, String to_type, String a_id, String ph_id) {
  566.         if (!checkUserType(caller_id, new int[]{U_READER})) {
  567.             return "error: access";
  568.         }
  569.         try {
  570.             String query =
  571.                     "INSERT INTO verifications(users_id, to_type, authors_id, publishing_houses_id) " +
  572.                     "VALUES (?, ?, ?, ?)";
  573.             PreparedStatement ps = con.prepareStatement(query);
  574.             ps.setLong(1, Long.parseLong(caller_id));
  575.             ps.setInt(2, Integer.parseInt(to_type));
  576.             if (a_id.equals("NULL")) {
  577.                 ps.setNull(3, Types.INTEGER);
  578.             } else {
  579.                 ps.setLong(3, Long.parseLong(a_id));
  580.             }
  581.             if (ph_id.equals("NULL")) {
  582.                 ps.setNull(4, Types.INTEGER);
  583.             } else {
  584.                 ps.setLong(4, Long.parseLong(ph_id));
  585.             }
  586.             ps.executeUpdate();
  587.             return "ok";
  588.         } catch (Exception e) {
  589.             return "error: " + e.getMessage();
  590.         }
  591.     }
  592.  
  593.     String addAuthorWorktime(String caller_id, String a_id, String o_id, String start, String finish) {
  594.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  595.             return "error: access";
  596.         }
  597.         try {
  598.             String query =
  599.                     "INSERT INTO authors_organizations(authors_id, organizations_id, start, finish) VALUES " +
  600.                     "(?, ?, ?, ?)";
  601.             PreparedStatement ps = con.prepareStatement(query);
  602.             ps.setLong(1, Long.parseLong(a_id));
  603.             ps.setLong(2, Long.parseLong(o_id));
  604.             ps.setDate(3, java.sql.Date.valueOf(start));
  605.             ps.setDate(4, java.sql.Date.valueOf(finish));
  606.             ps.executeUpdate();
  607.             return "ok";
  608.         } catch (Exception e) {
  609.             return "error: " + e.getMessage();
  610.         }
  611.     }
  612.  
  613.     String addAuthorToPubl(String caller_id, String p_id, String a_id) {
  614.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  615.             return "error: access";
  616.         }
  617.         String query = "INSERT INTO authors_publications(publications_id, authors_id) VALUES (?, ?)";
  618.         return execQueryWithTwoIds(query, p_id, a_id);
  619.     }
  620.  
  621.     String addEditorToPubl(String caller_id, String p_id, String e_id) {
  622.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  623.             return "error: access";
  624.         }
  625.         String query = "INSERT INTO editors_publications(publications_id, editors_id) VALUES (?, ?)";
  626.         return execQueryWithTwoIds(query, p_id, e_id);
  627.     }
  628.  
  629.     String addKeywordToPubl(String caller_id, String p_id, String k_id) {
  630.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  631.             return "error: access";
  632.         }
  633.         String query = "INSERT INTO publications_keywords(publications_id, keywords_id) VALUES (?, ?)";
  634.         return execQueryWithTwoIds(query, p_id, k_id);
  635.     }
  636.  
  637.     String addUdcToPubl(String caller_id, String p_id, String u_id) {
  638.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  639.             return "error: access";
  640.         }
  641.         String query = "INSERT INTO publications_udc_codes(publications_id, udc_codes_id) VALUES (?, ?)";
  642.         return execQueryWithTwoIds(query, p_id, u_id);
  643.     }
  644.  
  645.     String addUserToAuthor(String caller_id, String u_id, String a_id) {
  646.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  647.             return "error: access";
  648.         }
  649.         String query = "INSERT INTO users_authors(users_id, authors_id) VALUES (?, ?)";
  650.         return execQueryWithTwoIds(query, u_id, a_id);
  651.     }
  652.  
  653.     String addUserToPublHouse(String caller_id, String u_id, String ph_id) {
  654.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  655.             return "error: access";
  656.         }
  657.         String query = "INSERT INTO users_publishing_houses(users_id, publishing_houses_id) VALUES (?, ?)";
  658.         return execQueryWithTwoIds(query, u_id, ph_id);
  659.     }
  660.  
  661.     String deleteUser(String caller_id, String u_id) {
  662.         if (!checkUserType(caller_id, new int[]{U_ADMIN})) {
  663.             return "error: access";
  664.         }
  665.         return deleteById("users", u_id);
  666.     }
  667.  
  668.     String deletePublication(String caller_id, String p_id) {
  669.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  670.             return "error: access";
  671.         }
  672.         return deleteById("publications", p_id);
  673.     }
  674.  
  675.     String deleteSubject(String caller_id, String s_id) {
  676.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  677.             return "error: access";
  678.         }
  679.         return deleteById("subjects", s_id);
  680.     }
  681.  
  682.     String deleteMagazine(String caller_id, String m_id) {
  683.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  684.             return "error: access";
  685.         }
  686.         return deleteById("magazines", m_id);
  687.     }
  688.  
  689.     String deleteAuthor(String caller_id, String a_id) {
  690.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  691.             return "error: access";
  692.         }
  693.         return deleteById("authors", a_id);
  694.     }
  695.  
  696.     String deleteEditor(String caller_id, String e_id) {
  697.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  698.             return "error: access";
  699.         }
  700.         return deleteById("editors", e_id);
  701.     }
  702.  
  703.     String deleteOrganization(String caller_id, String o_id) {
  704.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  705.             return "error: access";
  706.         }
  707.         return deleteById("organizations", o_id);
  708.     }
  709.  
  710.     String deletePublHouse(String caller_id, String ph_id) {
  711.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  712.             return "error: access";
  713.         }
  714.         return deleteById("publishing_houses", ph_id);
  715.     }
  716.  
  717.     String deleteKeyword(String caller_id, String k_id) {
  718.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  719.             return "error: access";
  720.         }
  721.         return deleteById("keywords", k_id);
  722.     }
  723.  
  724.     String deleteUdc(String caller_id, String u_id) {
  725.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  726.             return "error: access";
  727.         }
  728.         return deleteById("udc_codes", u_id);
  729.     }
  730.  
  731.     String deleteVerf(String caller_id, String v_id) {
  732.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  733.             return "error: access";
  734.         }
  735.         return deleteById("verifications", v_id);
  736.     }
  737.  
  738.     String deleteAuthorWorktime(String caller_id, String ao_id) {
  739.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  740.             return "error: access";
  741.         }
  742.         return deleteById("authors_organizations", ao_id);
  743.     }
  744.  
  745.     private String deleteById(String table, String id) {
  746.         try {
  747.             String query = "DELETE FROM " + table + " WHERE " + table + "_id = ?";
  748.             PreparedStatement ps = con.prepareStatement(query);
  749.             ps.setLong(1, Long.parseLong(id));
  750.             ps.executeUpdate();
  751.             return "ok";
  752.         } catch (Exception e) {
  753.             return "error: " + e.getMessage();
  754.         }
  755.     }
  756.  
  757.     String deleteAuthorFromPubl(String caller_id, String p_id, String a_id) {
  758.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  759.             return "error: access";
  760.         }
  761.         String query = "DELETE FROM authors_publications WHERE publications_id = ? AND authors_id = ?";
  762.         return execQueryWithTwoIds(query, p_id, a_id);
  763.     }
  764.  
  765.     String deleteEditorFromPubl(String caller_id, String p_id, String e_id) {
  766.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  767.             return "error: access";
  768.         }
  769.         String query = "DELETE FROM editors_publications WHERE publications_id = ? AND editors_id = ?";
  770.         return execQueryWithTwoIds(query, p_id, e_id);
  771.     }
  772.  
  773.     String deleteKeywordFromPubl(String caller_id, String p_id, String k_id) {
  774.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  775.             return "error: access";
  776.         }
  777.         String query = "DELETE FROM publications_keywords WHERE publications_id = ? AND keywords_id = ?";
  778.         return execQueryWithTwoIds(query, p_id, k_id);
  779.     }
  780.  
  781.     String deleteUdcFromPubl(String caller_id, String p_id, String u_id) {
  782.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  783.             return "error: access";
  784.         }
  785.         String query = "DELETE FROM publications_udc_codes WHERE publications_id = ? AND udc_codes_id = ?";
  786.         return execQueryWithTwoIds(query, p_id, u_id);
  787.     }
  788.  
  789.     String changeUserInfo(String caller_id, String u_id, String name, String surname, String patronymic,
  790.                           String sex, String birth_date, String phone_number, String email) {
  791.         if (!(checkUserType(caller_id, new int[]{U_ADMIN}) || caller_id.equals(u_id))) {
  792.             return "error: access";
  793.         }
  794.         try {
  795.             String query =
  796.                     "UPDATE users " +
  797.                     "SET name = ?, surname = ?, patronymic = ?, sex = ?, birth_date = ?, phone_number = ?, email = ? " +
  798.                     "WHERE users_id = ?";
  799.             PreparedStatement ps = con.prepareStatement(query);
  800.             setFullNameAndSexInPs(ps, name, surname, patronymic, sex);
  801.             setDateInPs(ps, birth_date, 5);
  802.             if (phone_number.equals("NULL")) {
  803.                 ps.setNull(6, Types.BIGINT);
  804.             } else {
  805.                 ps.setLong(6, Long.parseLong(phone_number));
  806.             }
  807.             if (email.equals("NULL")) {
  808.                 ps.setNull(7, Types.VARCHAR);
  809.             } else {
  810.                 ps.setString(7, email);
  811.             }
  812.             ps.setLong(8, Long.parseLong(u_id));
  813.             ps.executeUpdate();
  814.             return "ok";
  815.         } catch (Exception e) {
  816.             return "error: " + e.getMessage();
  817.         }
  818.     }
  819.  
  820.     String changeUserPassword(String caller_id, String u_id, String password) {
  821.         if (!(checkUserType(caller_id, new int[]{U_ADMIN}) || caller_id.equals(u_id))) {
  822.             return "error: access";
  823.         }
  824.         try {
  825.             String query = "UPDATE users SET password = ? WHERE users_id = ?";
  826.             changeStringById(query, password, u_id);
  827.             return "ok";
  828.         } catch (Exception e) {
  829.             return "error: " + e.getMessage();
  830.         }
  831.     }
  832.  
  833.     String changeBook(String caller_id, String p_id, String title, String year) {
  834.         return changeBookAndDigestCommonPart(caller_id, p_id, title, year, P_BOOK);
  835.     }
  836.  
  837.     String changeDigest(String caller_id, String p_id, String title, String year) {
  838.         return changeBookAndDigestCommonPart(caller_id, p_id, title, year, P_DIGEST);
  839.     }
  840.  
  841.     private String changeBookAndDigestCommonPart(String u_id, String p_id, String title, String year, int type) {
  842.         try {
  843.             String error = checkChangePublPermissions(u_id, p_id, type);
  844.             if (!error.equals("ok")) {
  845.                 return error;
  846.             }
  847.             changePublTitle(p_id, title);
  848.             changePublishingYear(p_id, year);
  849.             return "ok";
  850.         } catch (Exception e) {
  851.             return "error: " + e.getMessage();
  852.         }
  853.     }
  854.  
  855.     private void changePublishingYear(String p_id, String year) throws Exception {
  856.         String query = "UPDATE publishing_years SET year = ? WHERE publications_id = ?";
  857.         PreparedStatement ps = con.prepareStatement(query);
  858.         ps.setInt(1, Integer.parseInt(year));
  859.         ps.setLong(2, Long.parseLong(p_id));
  860.         ps.executeUpdate();
  861.     }
  862.  
  863.     String changeArticle(String caller_id, String p_id, String title, String volume, String relase_number) {
  864.         try {
  865.             String error = checkChangePublPermissions(caller_id, p_id, P_ARTICLE);
  866.             if (!error.equals("ok")) {
  867.                 return error;
  868.             }
  869.             changePublTitle(p_id, title);
  870.             String query = "UPDATE magazine_article_info SET volume = ?, release_number = ? WHERE publications_id = ?";
  871.             PreparedStatement ps = con.prepareStatement(query);
  872.             ps.setInt(1, Integer.parseInt(volume));
  873.             ps.setInt(2, Integer.parseInt(relase_number));
  874.             ps.setLong(3, Long.parseLong(p_id));
  875.             ps.executeUpdate();
  876.             return "ok";
  877.         } catch (Exception e) {
  878.             return "error: " + e.getMessage();
  879.         }
  880.     }
  881.  
  882.     String changeTheses(String caller_id, String p_id, String title) {
  883.         return changeThesesAndDocsCommonPart(caller_id, p_id, title, P_THESES);
  884.     }
  885.  
  886.     String changeDocs(String caller_id, String p_id, String title) {
  887.         return changeThesesAndDocsCommonPart(caller_id, p_id, title, P_DOCS);
  888.     }
  889.  
  890.     private String changeThesesAndDocsCommonPart(String u_id, String p_id, String title, int type) {
  891.         try {
  892.             String error = checkChangePublPermissions(u_id, p_id, type);
  893.             if (!error.equals("ok")) {
  894.                 return error;
  895.             }
  896.             changePublTitle(p_id, title);
  897.             return "ok";
  898.         } catch (Exception e) {
  899.             return "error: " + e.getMessage();
  900.         }
  901.     }
  902.  
  903.     private String checkChangePublPermissions(String u_id, String p_id, int type) throws Exception {
  904.         if (!(checkUserType(u_id, new int[]{U_ADMIN, U_LIBRARIAN}) || isUserOwner(u_id, p_id))) {
  905.             return "error: access";
  906.         }
  907.         if (!checkPublType(p_id, type)) {
  908.             return "wrong publication type";
  909.         }
  910.         return "ok";
  911.     }
  912.  
  913.     private void changePublTitle(String p_id, String title) throws Exception {
  914.         String query = "UPDATE publications SET title = ? WHERE publications_id = ?";
  915.         PreparedStatement ps = con.prepareStatement(query);
  916.         ps.setString(1, title);
  917.         ps.setLong(2, Long.parseLong(p_id));
  918.         ps.executeUpdate();
  919.     }
  920.  
  921.     String changeSubject(String caller_id, String s_id, String title) {
  922.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  923.             return "error: access";
  924.         }
  925.         try {
  926.             String query = "UPDATE subjects SET subject = ? WHERE subjects_id = ?";
  927.             changeStringById(query, title, s_id);
  928.             return "ok";
  929.         } catch (Exception e) {
  930.             return "error: " + e.getMessage();
  931.         }
  932.     }
  933.  
  934.     String changeMagazine(String caller_id, String m_id, String title) {
  935.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  936.             return "error: access";
  937.         }
  938.         try {
  939.             String query = "UPDATE magazines SET title = ? WHERE magazines_id = ?";
  940.             changeStringById(query, title, m_id);
  941.             return "ok";
  942.         } catch (Exception e) {
  943.             return "error: " + e.getMessage();
  944.         }
  945.     }
  946.  
  947.     String changeOrganization(String caller_id, String o_id, String title, String address, String phone, String email) {
  948.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  949.             return "error: access";
  950.         }
  951.         String query =
  952.                 "UPDATE organizations SET title = ?, legal_address = ?, phone_number = ?, email = ? " +
  953.                 "WHERE organizations_id = ?";
  954.         return changeOrgOrPublHouseCommonPart(query, title, address, phone, email, o_id);
  955.     }
  956.  
  957.     String changePublHouse(String caller_id, String ph_id, String title, String address, String phone, String email) {
  958.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  959.             return "error: access";
  960.         }
  961.         String query =
  962.                 "UPDATE publishing_houses SET title = ?, legal_address = ?, phone_number = ?, email = ? " +
  963.                 "WHERE organizations_id = ?";
  964.         return changeOrgOrPublHouseCommonPart(query, title, address, phone, email, ph_id);
  965.     }
  966.  
  967.     private String changeOrgOrPublHouseCommonPart(String query, String title, String address, String phone,
  968.                                                   String email, String id) {
  969.         try {
  970.             PreparedStatement ps = con.prepareStatement(query);
  971.             setOrgOrPublHouseInfoInPS(ps, title, address, phone, email);
  972.             ps.setLong(4, Long.parseLong(id));
  973.             ps.executeUpdate();
  974.             return "ok";
  975.         } catch (Exception e) {
  976.             return "error: " + e.getMessage();
  977.         }
  978.     }
  979.  
  980.     String changeAuthor(String caller_id, String a_id, String name, String surname, String patronymic,
  981.                         String sex, String birth_date, String death_date, String phone_number, String email) {
  982.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  983.             return "error: access";
  984.         }
  985.         String query =
  986.                 "UPDATE authors SET name = ?, surname = ?, patronymic = ?, sex = ?, birth_date = ?, " +
  987.                 "death_date = ?, phone_number = ?, email = ? " +
  988.                 "WHERE authors_id = ?";
  989.         return changeAuthorOrEditorCommonPart(query, name, surname, patronymic, sex, birth_date, death_date,
  990.                 phone_number, email, a_id);
  991.     }
  992.  
  993.     String changeEditor(String caller_id, String e_id, String name, String surname, String patronymic, String sex,
  994.                         String birth_date, String death_date, String phone_number, String email) {
  995.         if (!checkUserType(caller_id, new int[]{U_ADMIN, U_LIBRARIAN})) {
  996.             return "error: access";
  997.         }
  998.         String query =
  999.                 "UPDATE editors SET name = ?, surname = ?, patronymic = ?, sex = ?, birth_date = ?, " +
  1000.                 "death_date = ?, phone_number = ?, email = ? " +
  1001.                 "WHERE editors_id = ?";
  1002.         return changeAuthorOrEditorCommonPart(query, name, surname, patronymic, sex, birth_date, death_date,
  1003.                 phone_number, email, e_id);
  1004.     }
  1005.  
  1006.     private String changeAuthorOrEditorCommonPart(String query, String name, String surname, String patronymic,
  1007.                                                   String sex, String birth_date, String death_date, String phone_number,
  1008.                                                   String email, String id) {
  1009.         try {
  1010.             PreparedStatement ps = con.prepareStatement(query);
  1011.             setAuthOrEditorInfoInPS(ps, name, surname, patronymic, sex, birth_date, death_date, phone_number, email);
  1012.             ps.setLong(9, Long.parseLong(id));
  1013.             ps.executeUpdate();
  1014.             return "ok";
  1015.         } catch (Exception e) {
  1016.             return "error: " + e.getMessage();
  1017.         }
  1018.     }
  1019.  
  1020.     private void changeStringById(String query, String string, String id) throws Exception {
  1021.         PreparedStatement ps = con.prepareStatement(query);
  1022.         ps.setString(1, string);
  1023.         ps.setLong(2, Long.parseLong(id));
  1024.         ps.executeUpdate();
  1025.     }
  1026.  
  1027.     private void setAuthOrEditorInfoInPS(PreparedStatement ps, String name, String surname, String patronymic,
  1028.                                          String sex, String birth_date, String death_date, String phone_number,
  1029.                                          String email) throws Exception {
  1030.         setFullNameAndSexInPs(ps, name, surname, patronymic, sex);
  1031.         setDateInPs(ps, birth_date, 5);
  1032.         setDateInPs(ps, death_date, 6);
  1033.         if (phone_number.equals("NULL")) {
  1034.             ps.setNull(7, Types.BIGINT);
  1035.         } else {
  1036.             ps.setLong(7, Long.parseLong(phone_number));
  1037.         }
  1038.         if (email.equals("NULL")) {
  1039.             ps.setNull(8, Types.VARCHAR);
  1040.         } else {
  1041.             ps.setString(8, email);
  1042.         }
  1043.     }
  1044.  
  1045.     private void setOrgOrPublHouseInfoInPS(PreparedStatement ps, String title, String address, String phone,
  1046.                                            String email) throws Exception {
  1047.         ps.setString(1, title);
  1048.         ps.setString(2, address);
  1049.         setPhoneAndEmailInPs(ps, phone, email, 3);
  1050.     }
  1051.  
  1052.     private void setDateInPs(PreparedStatement ps, String date, int index) throws Exception {
  1053.         if (date.equals("NULL")) {
  1054.             ps.setNull(index, Types.DATE);
  1055.         } else {
  1056.             ps.setDate(index, java.sql.Date.valueOf(date));
  1057.         }
  1058.     }
  1059.  
  1060.     private void setPhoneAndEmailInPs(PreparedStatement ps, String phone, String email,
  1061.                                       int start_index) throws Exception {
  1062.         if (phone.equals("NULL")) {
  1063.             ps.setNull(start_index, Types.BIGINT);
  1064.         } else {
  1065.             ps.setLong(start_index, Long.parseLong(phone));
  1066.         }
  1067.         if (email.equals("NULL")) {
  1068.             ps.setNull(start_index + 1, Types.VARCHAR);
  1069.         } else {
  1070.             ps.setString(start_index + 1, email);
  1071.         }
  1072.     }
  1073.  
  1074.     private void setFullNameAndSexInPs(PreparedStatement ps, String name, String surname,
  1075.                                        String patronymic, String sex) throws Exception {
  1076.         ps.setString(1, name);
  1077.         if (surname.equals("NULL")) {
  1078.             ps.setNull(2, Types.VARCHAR);
  1079.         } else {
  1080.             ps.setString(2, surname);
  1081.         }
  1082.         if (patronymic.equals("NULL")) {
  1083.             ps.setNull(3, Types.VARCHAR);
  1084.         } else {
  1085.             ps.setString(3, patronymic);
  1086.         }
  1087.         ps.setInt(4, Integer.parseInt(sex));
  1088.     }
  1089.  
  1090.     private boolean isUserOwner(String u_id, String p_id) throws Exception {
  1091.         String query =
  1092.                 "SELECT 1 " +
  1093.                 "FROM publications p " +
  1094.                 "LEFT JOIN authors_publications ap ON p.publications_id = ap.publications_id " +
  1095.                 "LEFT JOIN users_authors ua ON ua.authors_id = ap.authors_id " +
  1096.                 "LEFT JOIN publishing_houses_publications php ON p.publications_id = php.publications_id " +
  1097.                 "LEFT JOIN users_publishing_houses uph ON uph.publishing_houses_id = php.publishing_houses_id " +
  1098.                 "WHERE (ua.users_id = ? OR uph.users_id = ?) AND p.publications_id = ?";
  1099.         PreparedStatement ps = con.prepareStatement(query);
  1100.         ps.setLong(1, Long.parseLong(u_id));
  1101.         ps.setLong(2, Long.parseLong(u_id));
  1102.         ps.setLong(3, Long.parseLong(p_id));
  1103.         ResultSet rs = ps.executeQuery();
  1104.         return rs.next();
  1105.     }
  1106.  
  1107.     private boolean checkUserType(String u_id, int[] types) {
  1108.         int user_type;
  1109.         try {
  1110.             user_type = getUserType(u_id);
  1111.         } catch (Exception e) {
  1112.             return false;
  1113.         }
  1114.         for (int type : types) {
  1115.             if (user_type == type) {
  1116.                 return true;
  1117.             }
  1118.         }
  1119.         return false;
  1120.     }
  1121.  
  1122.     private boolean checkPublType(String p_id, int type) throws Exception {
  1123.         String query = "SELECT 1 FROM publications WHERE publications_id = ? AND type = ?";
  1124.         PreparedStatement ps = con.prepareStatement(query);
  1125.         ps.setLong(1, Long.parseLong(p_id));
  1126.         ps.setInt(2, type);
  1127.         ResultSet rs = ps.executeQuery();
  1128.         return rs.next();
  1129.     }
  1130.  
  1131.     private String execSelectQuery(String query) {
  1132.         try {
  1133.             Statement statement = con.createStatement();
  1134.             ResultSet rs = statement.executeQuery(query);
  1135.             return RSToString(rs);
  1136.         } catch (SQLException e) {
  1137.             return "error: " + e.getMessage();
  1138.         }
  1139.     }
  1140.  
  1141.     private String execPSWithId(String id, String query) {
  1142.         try {
  1143.             PreparedStatement ps = con.prepareStatement(query);
  1144.             ps.setLong(1, Long.parseLong(id));
  1145.             ResultSet rs = ps.executeQuery();
  1146.             return RSToString(rs);
  1147.         } catch (SQLException e) {
  1148.             return "error: " + e.getMessage();
  1149.         }
  1150.     }
  1151.  
  1152.     private String execQueryWithTwoIds(String query, String id1, String id2) {
  1153.         try {
  1154.             PreparedStatement ps = con.prepareStatement(query);
  1155.             ps.setLong(1, Long.parseLong(id1));
  1156.             ps.setLong(2, Long.parseLong(id2));
  1157.             ps.executeUpdate();
  1158.             return "ok";
  1159.         } catch (Exception e) {
  1160.             return "error: " + e.getMessage();
  1161.         }
  1162.     }
  1163.  
  1164.     private int getUserType(String id) throws Exception {
  1165.         PreparedStatement ps = con.prepareStatement("SELECT u.type FROM users u WHERE u.users_id = ?");
  1166.         ps.setInt(1, Integer.parseInt(id));
  1167.         ResultSet rs = ps.executeQuery();
  1168.         if (rs.next()) {
  1169.             return rs.getInt(1);
  1170.         } else {
  1171.             throw new Exception("No such user");
  1172.         }
  1173.     }
  1174.  
  1175.     private String RSToString(ResultSet rs) throws SQLException {
  1176.         int columnCount = rs.getMetaData().getColumnCount();
  1177.         ArrayList<ArrayList<String>> rows = new ArrayList<>();
  1178.         while (rs.next()) {
  1179.             ArrayList<String> row = new ArrayList<>();
  1180.             int i = 1;
  1181.             while (i <= columnCount) {
  1182.                 row.add(rs.getString(i++));
  1183.             }
  1184.             rows.add(row);
  1185.         }
  1186.         return new Gson().toJson(rows);
  1187.     }
  1188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement