comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 144:5369582d4fbf

Enable backend to delete users artifacts/trunk@1369 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 02 Mar 2011 17:43:25 +0000
parents b90e831d3dfe
children c0d025df722d
comparison
equal deleted inserted replaced
143:7e20702a90ed 144:5369582d4fbf
92 SQL.get("users.id.nextval"); 92 SQL.get("users.id.nextval");
93 93
94 public static final String SQL_USERS_INSERT = 94 public static final String SQL_USERS_INSERT =
95 SQL.get("users.insert"); 95 SQL.get("users.insert");
96 96
97 public static final String SQL_USERS_SELECT_ID_BY_GID =
98 SQL.get("users.select.id.by.gid");
99
97 public static final String SQL_USERS_SELECT_GID = 100 public static final String SQL_USERS_SELECT_GID =
98 SQL.get("users.select.gid"); 101 SQL.get("users.select.gid");
99 102
100 public static final String SQL_USERS_DELETE_ID = 103 public static final String SQL_USERS_DELETE_ID =
101 SQL.get("users.delete.id"); 104 SQL.get("users.delete.id");
103 public static final String SQL_USERS_DELETE_COLLECTIONS = 106 public static final String SQL_USERS_DELETE_COLLECTIONS =
104 SQL.get("users.delete.collections"); 107 SQL.get("users.delete.collections");
105 108
106 public static final String SQL_USER_SELECT_ALL = 109 public static final String SQL_USER_SELECT_ALL =
107 SQL.get("users.select.all"); 110 SQL.get("users.select.all");
111
112 public static final String SQL_USERS_COLLECTIONS =
113 SQL.get("users.collections");
114
115 public static final String SQL_USERS_COLLECTION_IDS =
116 SQL.get("users.collection.ids");
117
118 public static final String SQL_USERS_DELETE_ALL_COLLECTIONS =
119 SQL.get("users.delete.all.collections");
120
121 public static final String SQL_ARTIFACTS_IN_ONLY_COLLECTION_ONLY =
122 SQL.get("artifacts.in.one.collection.only");
123
124 public static final String SQL_OUTDATE_ARTIFACTS_COLLECTION =
125 SQL.get("outdate.artifacts.collection");
126
127 public static final String SQL_OUTDATE_ARTIFACTS_USER =
128 SQL.get("outdate.artifacts.user");
129
130 public static final String SQL_DELETE_USER_COLLECTION_ITEMS =
131 SQL.get("delete.user.collection.items");
108 132
109 /** The singleton.*/ 133 /** The singleton.*/
110 protected static Backend instance; 134 protected static Backend instance;
111 135
112 /** 136 /**
868 } 892 }
869 return null; 893 return null;
870 } 894 }
871 895
872 public void deleteUser(User user) { 896 public void deleteUser(User user) {
873 // TODO: implement me! 897
898 Connection conn = null;
899 ResultSet result = null;
900 PreparedStatement stmnt = null;
901
902 String identifier = user.identifier();
903
904 DataSource dataSource = DBConnection.getDataSource();
905 try {
906 conn = dataSource.getConnection();
907 try {
908 conn.setAutoCommit(false);
909
910 // fetch user id
911 stmnt = conn.prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
912
913 stmnt.setString(1, identifier);
914 result = stmnt.executeQuery();
915
916 if (!result.next()) { // No such user
917 return;
918 }
919
920 int id = result.getInt(1);
921
922 result.close(); result = null;
923 stmnt.close(); stmnt = null;
924
925 // outdate the artifacts exclusively used by the user
926
927 stmnt = conn.prepareStatement(SQL_OUTDATE_ARTIFACTS_USER);
928 stmnt.setInt(1, id);
929 stmnt.setInt(2, id);
930 stmnt.execute();
931
932 stmnt.close(); stmnt = null;
933
934 // delete the collection items of the user
935
936 stmnt = conn.prepareStatement(SQL_DELETE_USER_COLLECTION_ITEMS);
937 stmnt.setInt(1, id);
938 stmnt.execute();
939 stmnt.close(); stmnt = null;
940
941 // delete the collections of the user
942
943 stmnt = conn.prepareStatement(SQL_USERS_COLLECTIONS);
944 stmnt.setInt(1, id);
945 stmnt.execute();
946 stmnt.close(); stmnt = null;
947
948 // delete the user
949
950 stmnt = conn.prepareStatement(SQL_USERS_DELETE_ID);
951 stmnt.setInt(1, id);
952 stmnt.execute();
953
954 conn.commit();
955 }
956 catch (SQLException sqle) {
957 conn.rollback();
958 throw sqle;
959 }
960 }
961 catch (SQLException sqle) {
962 logger.error(sqle.getLocalizedMessage(), sqle);
963 }
964 finally {
965 if (result != null) {
966 try { result.close(); }
967 catch (SQLException sqle) {}
968 }
969 if (stmnt != null) {
970 try { stmnt.close(); }
971 catch (SQLException sqle) {}
972 }
973 if (conn != null) {
974 try { conn.close(); }
975 catch (SQLException sqle) {}
976 }
977 }
874 } 978 }
875 979
876 public User getUser(String identifier) { 980 public User getUser(String identifier) {
877 // TODO: implement me! 981 // TODO: implement me!
878 return null; 982 return null;

http://dive4elements.wald.intevation.org