Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 138:b90e831d3dfe
Call database to create a new user.
artifacts/trunk@1363 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 02 Mar 2011 11:24:03 +0000 |
parents | 2950c6011afa |
children | 5369582d4fbf |
comparison
equal
deleted
inserted
replaced
137:f92ad0e40df2 | 138:b90e831d3dfe |
---|---|
10 | 10 |
11 import de.intevation.artifacts.Artifact; | 11 import de.intevation.artifacts.Artifact; |
12 import de.intevation.artifacts.ArtifactFactory; | 12 import de.intevation.artifacts.ArtifactFactory; |
13 import de.intevation.artifacts.ArtifactSerializer; | 13 import de.intevation.artifacts.ArtifactSerializer; |
14 import de.intevation.artifacts.User; | 14 import de.intevation.artifacts.User; |
15 | |
16 import de.intevation.artifacts.common.utils.XMLUtils; | |
15 | 17 |
16 import java.sql.Connection; | 18 import java.sql.Connection; |
17 import java.sql.PreparedStatement; | 19 import java.sql.PreparedStatement; |
18 import java.sql.ResultSet; | 20 import java.sql.ResultSet; |
19 import java.sql.SQLException; | 21 import java.sql.SQLException; |
791 | 793 |
792 public User createUser( | 794 public User createUser( |
793 String name, | 795 String name, |
794 Document role | 796 Document role |
795 ) { | 797 ) { |
796 // TODO: implement me! | 798 Connection connection = null; |
799 PreparedStatement stmnt_next_id = null; | |
800 PreparedStatement stmnt_insert = null; | |
801 ResultSet res_id = null; | |
802 | |
803 String identifier = newIdentifier(); | |
804 | |
805 DataSource dataSource = DBConnection.getDataSource(); | |
806 try { | |
807 connection = dataSource.getConnection(); | |
808 try { | |
809 connection.setAutoCommit(false); | |
810 | |
811 stmnt_next_id = connection.prepareStatement(SQL_USERS_NEXT_ID); | |
812 stmnt_insert = connection.prepareStatement(SQL_USERS_INSERT); | |
813 | |
814 res_id = stmnt_next_id.executeQuery(); | |
815 | |
816 if (!res_id.next()) { | |
817 throw new RuntimeException("No id generated"); | |
818 } | |
819 | |
820 int id = res_id.getInt(1); | |
821 | |
822 stmnt_insert.setInt(1, id); | |
823 stmnt_insert.setString(2, identifier); | |
824 stmnt_insert.setString(3, name); | |
825 | |
826 byte [] roleData = role == null | |
827 ? null | |
828 : XMLUtils.toByteArray(role); | |
829 | |
830 if (roleData == null) { | |
831 stmnt_insert.setNull(4, Types.BIGINT); | |
832 } | |
833 else { | |
834 stmnt_insert.setBytes(4, roleData); | |
835 } | |
836 | |
837 stmnt_insert.execute(); | |
838 | |
839 connection.commit(); | |
840 | |
841 return new DefaultUser(identifier, name, role); | |
842 } | |
843 catch (SQLException sqle) { | |
844 connection.rollback(); | |
845 throw sqle; | |
846 } | |
847 } | |
848 catch (SQLException sqle) { | |
849 logger.error(sqle.getLocalizedMessage(), sqle); | |
850 } | |
851 finally { | |
852 if (res_id != null) { | |
853 try { res_id.close(); } | |
854 catch (SQLException sqle) {} | |
855 } | |
856 if (stmnt_insert != null) { | |
857 try { stmnt_insert.close(); } | |
858 catch (SQLException sqle) {} | |
859 } | |
860 if (stmnt_next_id != null) { | |
861 try { stmnt_next_id.close(); } | |
862 catch (SQLException sqle) {} | |
863 } | |
864 if (connection != null) { | |
865 try { connection.close(); } | |
866 catch (SQLException sqle) {} | |
867 } | |
868 } | |
797 return null; | 869 return null; |
798 } | 870 } |
799 | 871 |
800 public void deleteUser(User user) { | 872 public void deleteUser(User user) { |
801 // TODO: implement me! | 873 // TODO: implement me! |