class SoapFolderService extends AbstractService implements FolderService {

	@Override
	public void rename(String sid, long folderId, String name) throws Exception {
		User user = validateSession(sid);
		FolderDAO folderDao = (FolderDAO) Context.get().getBean(FolderDAO.class);
		if (!folderDao.isPermissionEnabled(Permission.RENAME, folderId, user.getId())) {
			throw new Exception("user does't have rename permission");
		}

		long rootId = folderDao.findRoot(user.getTenantId()).getId();
		if (folderId == rootId)
			throw new Exception("cannot rename the root folder");

		if (folderId == rootId)
			throw new Exception("cannot rename the Default workspace");

		Folder folder = folderDao.findById(folderId);
		if (folder == null)
			throw new Exception(String.format("cannot find folder %s", folderId));

		folderDao.initialize(folder);

		List<Folder> folders = folderDao.findByNameAndParentId(name, folder.getParentId());
		if (folders.size() > 0 && folders.get(0).getId() != folder.getId()) {
			throw new Exception(String.format("duplicate folder name %s", name));
		} else {
			// Add a folder history entry
			FolderHistory transaction = new FolderHistory();
			transaction.setUser(user);
			transaction.setEvent(FolderEvent.RENAMED.toString());
			transaction.setSessionId(sid);
			transaction.setFilenameOld(folder.getName());
			transaction.setPathOld(folderDao.computePathExtended(folderId));

			folder.setName(name);
			folderDao.store(folder, transaction);
		}
	}

}