Revision f7c44c33 src/gr/ebs/gss/server/ejb/ExternalAPIBean.java

b/src/gr/ebs/gss/server/ejb/ExternalAPIBean.java
153 153
	}
154 154

  
155 155
	@Override
156
	public FolderDTO getRootFolder(Long userId) throws ObjectNotFoundException {
156
	public Folder getRootFolder(Long userId) throws ObjectNotFoundException {
157 157
		if (userId == null)
158 158
			throw new ObjectNotFoundException("No user specified");
159 159
		Folder folder = dao.getRootFolder(userId);
160
		return folder.getDTO();
160
		return folder;
161 161
	}
162 162

  
163 163
	@Override
164
	public FolderDTO getFolder(final Long userId, final Long folderId) throws ObjectNotFoundException, InsufficientPermissionsException {
164
	public Folder getFolder(final Long userId, final Long folderId) throws ObjectNotFoundException, InsufficientPermissionsException {
165 165
		if (userId == null)
166 166
			throw new ObjectNotFoundException("No user specified");
167 167
		if (folderId == null)
......
171 171
		// Check permissions
172 172
		if (!folder.hasReadPermission(user))
173 173
			throw new InsufficientPermissionsException("You don't have the permissions to read this folder");
174
		return folder.getDTO();
174
		return folder;
175 175
	}
176 176

  
177 177
	@Override
......
187 187
	}
188 188

  
189 189
	@Override
190
	public GroupDTO getGroup(final Long groupId) throws ObjectNotFoundException {
190
	public Group getGroup(final Long groupId) throws ObjectNotFoundException {
191 191
		if (groupId == null)
192 192
			throw new ObjectNotFoundException("No group specified");
193 193
		final Group group = dao.getEntityById(Group.class, groupId);
194
		return group.getDTO();
194
		return group;
195 195
	}
196 196

  
197 197
	@Override
198
	public GroupDTO getGroup(Long userId, String name) throws ObjectNotFoundException {
198
	public Group getGroup(Long userId, String name) throws ObjectNotFoundException {
199 199
		if (userId == null)
200 200
			throw new ObjectNotFoundException("No user specified");
201 201
		if (name == null)
......
204 204
		List<Group> groups = user.getGroupsSpecified();
205 205
		for (Group group: groups)
206 206
			if (group.getName().equals(name))
207
				return group.getDTO();
207
				return group;
208 208
		throw new ObjectNotFoundException("Group " + name + " not found");
209 209
	}
210 210

  
211 211
	@Override
212
	public List<GroupDTO> getGroups(final Long userId) throws ObjectNotFoundException {
212
	public List<Group> getGroups(final Long userId) throws ObjectNotFoundException {
213 213
		if (userId == null)
214 214
			throw new ObjectNotFoundException("No user specified");
215 215
		final List<Group> groups = dao.getGroups(userId);
216
		final List<GroupDTO> result = new ArrayList<GroupDTO>();
217
		for (final Group g : groups)
218
			result.add(g.getDTO());
219
		return result;
216
		return groups;
220 217
	}
221 218

  
222 219
	@Override
223
	public List<FileHeaderDTO> getFiles(Long userId, Long folderId, boolean ignoreDeleted)
220
	public List<FileHeader> getFiles(Long userId, Long folderId, boolean ignoreDeleted)
224 221
			throws ObjectNotFoundException, InsufficientPermissionsException {
225 222
		// Validate.
226 223
		if (userId == null)
......
231 228
		Folder folder = dao.getEntityById(Folder.class, folderId);
232 229
		if (!folder.hasReadPermission(user))
233 230
			throw new InsufficientPermissionsException("You don't have the permissions to read this folder");
234
		// Do the actual work.
235
		List<FileHeaderDTO> result = new ArrayList<FileHeaderDTO>();
236 231
		List<FileHeader> files = dao.getFiles(folderId, userId, ignoreDeleted);
237
		for (FileHeader f : files)
238
			result.add(f.getDTO());
239
		return result;
232
		return files;
240 233
	}
241 234

  
242 235
	@Override
......
256 249
	}
257 250

  
258 251
	@Override
259
	public FolderDTO createFolder(Long userId, Long parentId, String name)
252
	public Folder createFolder(Long userId, Long parentId, String name)
260 253
			throws DuplicateNameException, ObjectNotFoundException, InsufficientPermissionsException {
261 254
		// Validate.
262 255
		if (userId == null)
......
294 287
	 * @param creator
295 288
	 * @return the new folder
296 289
	 */
297
	private FolderDTO createFolder(String name, Folder parent, User creator) {
290
	private Folder createFolder(String name, Folder parent, User creator) {
298 291
		Folder folder = new Folder();
299 292
		folder.setName(name);
300 293
		if (parent != null) {
......
335 328
			folder.setReadForAll(parent.isReadForAll());
336 329

  
337 330
		dao.create(folder);
338
		return folder.getDTO();
331
		return folder;
339 332
	}
340 333

  
341 334
	@Override
......
401 394
	}
402 395

  
403 396
	@Override
404
	public FolderDTO updateFolder(Long userId, Long folderId, String folderName,
397
	public Folder updateFolder(Long userId, Long folderId, String folderName,
405 398
				Boolean readForAll,
406
				Set<PermissionDTO> permissions)
399
				Set<Permission> permissions)
407 400
			throws InsufficientPermissionsException, ObjectNotFoundException,
408 401
			DuplicateNameException {
409 402

  
......
440 433
		folder.getAuditInfo().setModifiedBy(user);
441 434
		dao.update(folder);
442 435
		touchParentFolders(folder, user, new Date());
443
		return folder.getDTO();
436
		return folder;
444 437
	}
445 438

  
446 439
	@Override
......
638 631
	@Override
639 632
	public void updateFile(Long userId, Long fileId, String name,
640 633
				String tagSet, Date modificationDate, Boolean versioned,
641
				Boolean readForAll,	Set<PermissionDTO> permissions)
634
				Boolean readForAll,	Set<Permission> permissions)
642 635
			throws DuplicateNameException, ObjectNotFoundException,	InsufficientPermissionsException {
643 636
		if (userId == null)
644 637
			throw new ObjectNotFoundException("No user specified");
......
774 767
	}
775 768

  
776 769
	@Override
777
	public FileHeaderDTO getFile(Long userId, Long fileId) throws ObjectNotFoundException, InsufficientPermissionsException {
770
	public FileHeader getFile(Long userId, Long fileId) throws ObjectNotFoundException, InsufficientPermissionsException {
778 771
		if (userId == null)
779 772
			throw new ObjectNotFoundException("No user specified");
780 773
		if (fileId == null)
......
783 776
		final FileHeader file = dao.getEntityById(FileHeader.class, fileId);
784 777
		if (!file.hasReadPermission(user) && !file.getFolder().hasReadPermission(user))
785 778
			throw new InsufficientPermissionsException("You don't have the necessary permissions");
786
		return file.getDTO();
779
		return file;
787 780
	}
788 781

  
789 782
	@Override
......
817 810
			return getRootFolder(owner.getId());
818 811
		// Store the last element, since it requires special handling.
819 812
		String lastElement = pathElements.remove(pathElements.size() - 1);
820
		FolderDTO cursor = getRootFolder(owner.getId());
813
		Folder cursor = getRootFolder(owner.getId());
821 814
		// Traverse and verify the specified folder path.
822 815
		for (String pathElement : pathElements) {
823 816
			cursor = getFolder(cursor.getId(), pathElement);
......
828 821
		// Use the lastElement to retrieve the actual resource.
829 822
		Object resource = null;
830 823
		try {
831
			FileHeaderDTO file = getFile(cursor.getId(), lastElement);
824
			FileHeader file = getFile(cursor.getId(), lastElement);
832 825
			if (ignoreDeleted && file.isDeleted())
833 826
				throw new ObjectNotFoundException("Resource not found");
834 827
			resource = file;
......
854 847
	 *             found, with the exception message mentioning the precise
855 848
	 *             problem
856 849
	 */
857
	private FileHeaderDTO getFile(Long folderId, String name) throws ObjectNotFoundException {
850
	private FileHeader getFile(Long folderId, String name) throws ObjectNotFoundException {
858 851
		if (folderId == null)
859 852
			throw new ObjectNotFoundException("No parent folder specified");
860 853
		if (StringUtils.isEmpty(name))
861 854
			throw new ObjectNotFoundException("No file specified");
862 855

  
863 856
		FileHeader file = dao.getFile(folderId, name);
864
		return file.getDTO();
857
		return file;
865 858
	}
866 859

  
867 860
	/**
......
875 868
	 *             found, with the exception message mentioning the precise
876 869
	 *             problem
877 870
	 */
878
	private FolderDTO getFolder(Long parentId, String name) throws ObjectNotFoundException {
871
	private Folder getFolder(Long parentId, String name) throws ObjectNotFoundException {
879 872
		if (parentId == null)
880 873
			throw new ObjectNotFoundException("No parent folder specified");
881 874
		if (StringUtils.isEmpty(name))
882 875
			throw new ObjectNotFoundException("No folder specified");
883 876

  
884 877
		Folder folder = dao.getFolder(parentId, name);
885
		return folder.getDTO();
878
		return folder;
886 879
	}
887 880

  
888 881
	private FileHeaderDTO updateFileContents(Long userId, Long fileId, String mimeType, InputStream resourceInputStream) throws ObjectNotFoundException, GSSIOException, InsufficientPermissionsException, QuotaExceededException {
......
1460 1453
	}
1461 1454

  
1462 1455
	@Override
1463
	public Set<PermissionDTO> getFolderPermissions(Long userId, Long folderId) throws ObjectNotFoundException, InsufficientPermissionsException {
1456
	public Set<Permission> getFolderPermissions(Long userId, Long folderId) throws ObjectNotFoundException, InsufficientPermissionsException {
1464 1457
		if (userId == null)
1465 1458
			throw new ObjectNotFoundException("No user specified");
1466 1459
		if (folderId == null)
......
1470 1463
		if(!folder.hasReadPermission(user))
1471 1464
			throw new InsufficientPermissionsException("You don't have the necessary permissions");
1472 1465
		Set<Permission> perms = folder.getPermissions();
1473
		Set<PermissionDTO> result = new LinkedHashSet<PermissionDTO>();
1466
		Set<Permission> result = new LinkedHashSet<Permission>();
1474 1467
		for (Permission perm : perms)
1475 1468
			if (perm.getUser() != null && perm.getUser().getId().equals(folder.getOwner().getId()))
1476
				result.add(perm.getDTO());
1469
				result.add(perm);
1477 1470
		for (Permission perm : perms)
1478 1471
			if (perm.getUser() != null && perm.getUser().getId().equals(folder.getOwner().getId())) {
1479 1472
			} else
1480
				result.add(perm.getDTO());
1473
				result.add(perm);
1481 1474
		return result;
1482 1475

  
1483 1476
	}
......
1492 1485
	 * @throws ObjectNotFoundException
1493 1486
	 * @throws InsufficientPermissionsException
1494 1487
	 */
1495
	private void setFolderPermissions(User user, Folder folder, Set<PermissionDTO> permissions) throws ObjectNotFoundException, InsufficientPermissionsException {
1488
	private void setFolderPermissions(User user, Folder folder, Set<Permission> permissions) throws ObjectNotFoundException, InsufficientPermissionsException {
1496 1489
		if (permissions != null && !permissions.isEmpty()) {
1497 1490
			User owner = folder.getOwner();
1498
			PermissionDTO ownerPerm = null;
1499
			for (PermissionDTO dto : permissions)
1500
				if (dto.getUser() != null && dto.getUser().getId().equals(owner.getId())) {
1501
					ownerPerm = dto;
1491
			Permission ownerPerm = null;
1492
			for (Permission perm : permissions)
1493
				if (perm.getUser() != null && perm.getUser().getId().equals(owner.getId())) {
1494
					ownerPerm = perm;
1502 1495
					break;
1503 1496
				}
1504 1497
			if (ownerPerm == null || !ownerPerm.hasRead() || !ownerPerm.hasWrite() || !ownerPerm.hasModifyACL())
......
1507 1500
			for (Permission perm: folder.getPermissions())
1508 1501
				dao.delete(perm);
1509 1502
			folder.getPermissions().clear();
1510
			for (PermissionDTO dto : permissions) {
1503
			for (Permission p : permissions) {
1511 1504
				// Skip 'empty' permission entries.
1512
				if (!dto.getRead() && !dto.getWrite() && !dto.getModifyACL()) continue;
1513
				folder.addPermission(getPermission(dto));
1505
				if (!p.getRead() && !p.getWrite() && !p.getModifyACL()) continue;
1506
				folder.addPermission(getPermission(p));
1514 1507
			}
1515 1508
			dao.update(folder);
1516 1509
			for (FileHeader file : folder.getFiles()) {
......
1524 1517
		}
1525 1518
	}
1526 1519

  
1527
	private Permission getPermission(PermissionDTO dto) throws ObjectNotFoundException {
1520
	private Permission getPermission(Permission perm) throws ObjectNotFoundException {
1528 1521
		Permission res = new Permission();
1529
		if (dto.getGroup() != null)
1530
			res.setGroup(dao.getEntityById(Group.class, dto.getGroup().getId()));
1531
		else if (dto.getUser() != null)
1532
			if (dto.getUser().getId() == null)
1533
				res.setUser(dao.getUser(dto.getUser().getUsername()));
1522
		if (perm.getGroup() != null)
1523
			res.setGroup(dao.getEntityById(Group.class, perm.getGroup().getId()));
1524
		else if (perm.getUser() != null)
1525
			if (perm.getUser().getId() == null)
1526
				res.setUser(dao.getUser(perm.getUser().getUsername()));
1534 1527
			else
1535
				res.setUser(dao.getEntityById(User.class, dto.getUser().getId()));
1536
		res.setRead(dto.hasRead());
1537
		res.setWrite(dto.hasWrite());
1538
		res.setModifyACL(dto.hasModifyACL());
1528
				res.setUser(dao.getEntityById(User.class, perm.getUser().getId()));
1529
		res.setRead(perm.hasRead());
1530
		res.setWrite(perm.hasWrite());
1531
		res.setModifyACL(perm.hasModifyACL());
1539 1532
		return res;
1540 1533
	}
1541 1534

  
......
1627 1620
	}
1628 1621

  
1629 1622
	@Override
1630
	public Set<PermissionDTO> getFilePermissions(Long userId, Long fileId) throws ObjectNotFoundException, InsufficientPermissionsException {
1623
	public Set<Permission> getFilePermissions(Long userId, Long fileId) throws ObjectNotFoundException, InsufficientPermissionsException {
1631 1624
		if (userId == null)
1632 1625
			throw new ObjectNotFoundException("No user specified");
1633 1626
		if (fileId == null)
......
1637 1630
		if(!folder.hasReadPermission(user))
1638 1631
			throw new InsufficientPermissionsException("You don't have the necessary permissions");
1639 1632
		Set<Permission> perms = folder.getPermissions();
1640
		Set<PermissionDTO> result = new LinkedHashSet<PermissionDTO>();
1633
		Set<Permission> result = new LinkedHashSet<Permission>();
1641 1634
		for (Permission perm : perms)
1642 1635
			if (perm.getUser() != null && perm.getUser().getId().equals(folder.getOwner().getId()))
1643
				result.add(perm.getDTO());
1636
				result.add(perm);
1644 1637
		for (Permission perm : perms)
1645 1638
			if (perm.getUser() != null && perm.getUser().getId().equals(folder.getOwner().getId())) {
1646 1639
			} else
1647
				result.add(perm.getDTO());
1640
				result.add(perm);
1648 1641
		return result;
1649 1642
	}
1650 1643

  
......
1659 1652
	 * @throws InsufficientPermissionsException
1660 1653
	 */
1661 1654
	private void setFilePermissions(FileHeader file,
1662
				Set<PermissionDTO> permissions)
1655
				Set<Permission> permissions)
1663 1656
			throws ObjectNotFoundException, InsufficientPermissionsException {
1664 1657
		if (permissions != null && !permissions.isEmpty()) {
1665
			PermissionDTO ownerPerm = null;
1666
			for (PermissionDTO dto : permissions)
1667
				if (dto.getUser() != null && dto.getUser().getId().equals(file.getOwner().getId())) {
1668
					ownerPerm = dto;
1658
			Permission ownerPerm = null;
1659
			for (Permission perm : permissions)
1660
				if (perm.getUser() != null && perm.getUser().getId().equals(file.getOwner().getId())) {
1661
					ownerPerm = perm;
1669 1662
					break;
1670 1663
				}
1671 1664
			if (ownerPerm == null || !ownerPerm.hasRead() || !ownerPerm.hasWrite() || !ownerPerm.hasModifyACL())
......
1674 1667
			for (Permission perm: file.getPermissions())
1675 1668
				dao.delete(perm);
1676 1669
			file.getPermissions().clear();
1677
			for (PermissionDTO dto : permissions) {
1670
			for (Permission perm : permissions) {
1678 1671
				// Skip 'empty' permission entries.
1679
				if (!dto.getRead() && !dto.getWrite() && !dto.getModifyACL()) continue;
1680
				file.addPermission(getPermission(dto));
1672
				if (!perm.getRead() && !perm.getWrite() && !perm.getModifyACL()) continue;
1673
				file.addPermission(getPermission(perm));
1681 1674
			}
1682 1675
			dao.flush();
1683 1676
		}
......
2138 2131
	}
2139 2132

  
2140 2133
	@Override
2141
	public FileHeaderDTO createFile(Long userId, Long folderId, String name, String mimeType, long fileSize, String filePath)
2134
	public FileHeader createFile(Long userId, Long folderId, String name, String mimeType, long fileSize, String filePath)
2142 2135
			throws DuplicateNameException, ObjectNotFoundException, GSSIOException,
2143 2136
			InsufficientPermissionsException, QuotaExceededException {
2144 2137
		// Validate.
......
2204 2197
		dao.flush();
2205 2198
		indexFile(file.getId(), false);
2206 2199

  
2207
		return file.getDTO();
2200
		return file;
2208 2201
	}
2209 2202

  
2210 2203
	@Override
2211
	public FileHeaderDTO updateFileContents(Long userId, Long fileId, String mimeType, long fileSize, String filePath) throws ObjectNotFoundException, GSSIOException, InsufficientPermissionsException, QuotaExceededException {
2204
	public FileHeader updateFileContents(Long userId, Long fileId, String mimeType, long fileSize, String filePath) throws ObjectNotFoundException, GSSIOException, InsufficientPermissionsException, QuotaExceededException {
2212 2205
		if (userId == null)
2213 2206
			throw new ObjectNotFoundException("No user specified");
2214 2207
		if (fileId == null)
......
2241 2234
		touchParentFolders(parent, owner, new Date());
2242 2235

  
2243 2236
		indexFile(fileId, false);
2244
		return file.getDTO();
2237
		return file;
2245 2238
	}
2246 2239

  
2247 2240
	/**
......
2449 2442
	}
2450 2443

  
2451 2444
	@Override
2452
	public FileBodyDTO getFileVersion(Long userId, Long fileId, int version)
2445
	public FileBody getFileVersion(Long userId, Long fileId, int version)
2453 2446
			throws ObjectNotFoundException, InsufficientPermissionsException {
2454 2447
		if (userId == null)
2455 2448
			throw new ObjectNotFoundException("No user specified");
......
2462 2455
		if (!file.hasReadPermission(user) && !file.getFolder().hasReadPermission(user))
2463 2456
			throw new InsufficientPermissionsException("You don't have the necessary permissions");
2464 2457
		FileBody body = dao.getFileVersion(fileId, version);
2465
		return body.getDTO();
2458
		return body;
2466 2459
	}
2467 2460

  
2468 2461
	@Override

Also available in: Unified diff