1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend.dao.impl; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.dao.AnnotationDao; |
---|
21 | import eu.dasish.annotation.backend.dao.PermissionsDao; |
---|
22 | import eu.dasish.annotation.backend.dao.UserDao; |
---|
23 | import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier; |
---|
24 | import eu.dasish.annotation.backend.identifiers.UserIdentifier; |
---|
25 | import eu.dasish.annotation.schema.Permission; |
---|
26 | import eu.dasish.annotation.schema.UserWithPermission; |
---|
27 | import java.sql.ResultSet; |
---|
28 | import java.sql.SQLException; |
---|
29 | import java.util.HashMap; |
---|
30 | import java.util.List; |
---|
31 | import java.util.Map; |
---|
32 | import javax.sql.DataSource; |
---|
33 | import org.springframework.beans.factory.annotation.Autowired; |
---|
34 | import org.springframework.jdbc.core.RowMapper; |
---|
35 | |
---|
36 | /** |
---|
37 | * |
---|
38 | * @author olhsha |
---|
39 | */ |
---|
40 | public class JdbcPermissionsDao extends JdbcResourceDao implements PermissionsDao { |
---|
41 | |
---|
42 | @Autowired |
---|
43 | private UserDao jdbcUserDao; |
---|
44 | |
---|
45 | @Autowired |
---|
46 | private AnnotationDao jdbcAnnotationDao; |
---|
47 | |
---|
48 | |
---|
49 | public JdbcPermissionsDao(DataSource dataSource) { |
---|
50 | setDataSource(dataSource); |
---|
51 | } |
---|
52 | |
---|
53 | /////////////////////////////////////////////////////////////////// |
---|
54 | @Override |
---|
55 | public List<UserWithPermission> retrievePermissions(Number annotationId) { |
---|
56 | if (annotationId == null) { |
---|
57 | return null; |
---|
58 | } |
---|
59 | String sql = "SELECT " + principal_id + "," + permission + " FROM " + permissionsTableName + " WHERE " + annotation_id + " = ?"; |
---|
60 | List<UserWithPermission> result = getSimpleJdbcTemplate().query(sql, principalsPermissionsRowMapper, annotationId.toString()); |
---|
61 | return result; |
---|
62 | } |
---|
63 | private final RowMapper<UserWithPermission> principalsPermissionsRowMapper = new RowMapper<UserWithPermission>() { |
---|
64 | @Override |
---|
65 | public UserWithPermission mapRow(ResultSet rs, int rowNumber) throws SQLException { |
---|
66 | UserWithPermission result = new UserWithPermission(); |
---|
67 | result.setRef((jdbcUserDao.getExternalID(rs.getInt(principal_id))).toString()); |
---|
68 | result.setPermission(Permission.fromValue(rs.getString(permission))); |
---|
69 | return result; |
---|
70 | } |
---|
71 | }; |
---|
72 | |
---|
73 | ///////////////////////////////////////////////////////////////////////////////////////// |
---|
74 | @Override |
---|
75 | public int addAnnotationPrincipalPermission(AnnotationIdentifier annotationIdenitifier, UserIdentifier userIdentifier, Permission permission) throws SQLException { |
---|
76 | Map<String, Object> paramsPermissions = new HashMap<String, Object>(); |
---|
77 | paramsPermissions.put("annotationId", jdbcAnnotationDao.getInternalID(annotationIdenitifier)); |
---|
78 | paramsPermissions.put("principalId", jdbcUserDao.getInternalID(userIdentifier)); |
---|
79 | paramsPermissions.put("status", permission.value()); |
---|
80 | String sqlUpdatePermissionTable = "INSERT INTO " + permissionsTableName + " (" + annotation_id + "," + principal_id + "," + permission + ") VALUES (:annotationId, :principalId, :status)"; |
---|
81 | final int affectedPermissions = getSimpleJdbcTemplate().update(sqlUpdatePermissionTable, paramsPermissions); |
---|
82 | return affectedPermissions; |
---|
83 | } |
---|
84 | |
---|
85 | @Override |
---|
86 | public int removeAnnotation(Number annotationID){ |
---|
87 | String sqlPermissions = "DELETE FROM " + permissionsTableName + " where "+annotation_id + " = ?"; |
---|
88 | int affectedPermissions = getSimpleJdbcTemplate().update(sqlPermissions, annotationID); |
---|
89 | return affectedPermissions; |
---|
90 | } |
---|
91 | ///////////////////////////////////////////////////////////////////////////////// |
---|
92 | //TODO replace name "user" in the scheme beacuse it is misleading. E.g. replace it with |
---|
93 | // getUser actual gives you the list of PAIRS (user, permission) that are refferred from an annotation |
---|
94 | // @Override |
---|
95 | // public PermissionList makeFreshPermissionList(UserIdentifier owner) { |
---|
96 | // PermissionList result = new PermissionList(); |
---|
97 | // |
---|
98 | // result.setURI((new PermissionListIdentifier()).toString()); |
---|
99 | // |
---|
100 | // UserWithPermission idOwner = new UserWithPermission(); |
---|
101 | // idOwner.setPermission(Permission.fromValue("owner")); |
---|
102 | // idOwner.setRef(owner.toString()); |
---|
103 | // |
---|
104 | // result.getUser().add(idOwner); |
---|
105 | // return result; |
---|
106 | // } |
---|
107 | |
---|
108 | } |
---|