class SoapFolderService extends AbstractService implements FolderService {

	@Override
	public WSFolder create(String sid, WSFolder folder) throws Exception {
		User user = validateSession(sid);

		try {
			FolderDAO folderDao = (FolderDAO) Context.get().getBean(FolderDAO.class);
			Folder parentFolder = folderDao.findById(folder.getParentId());
			if (parentFolder == null)
				throw new Exception(String.format("A parent folder with id %s was not found.", folder.getParentId()));
			checkPermission(Permission.ADD, user, folder.getParentId());

			// Add a folder history entry
			FolderHistory transaction = new FolderHistory();
			transaction.setUser(user);
			transaction.setSessionId(sid);

			Folder folderVO = new Folder();
			folderVO.setTenantId(user.getTenantId());
			folderVO.setName(folder.getName());
			folderVO.setDescription(folder.getDescription());
			folderVO.setType(folder.getType());
			folderVO.setPosition(folder.getPosition());
			folderVO.setTemplateLocked(folder.getTemplateLocked());
			folderVO.setHidden(folder.getHidden());
			folderVO.setFoldRef(folder.getFoldRef());
			folderVO.setStorage(folder.getStorage());
			folderVO.setSecurityRef(folder.getSecurityRef());
			folderVO.setFoldRef(folder.getFoldRef());
			folderVO.setOcrTemplateId(folder.getOcrTemplateId());
			folderVO.setBarcodeTemplateId(folder.getBarcodeTemplateId());
			
			Set<String> tagsSet = new TreeSet<String>();
			if (folder.getTags() != null) {
				for (int i = 0; i < folder.getTags().length; i++) {
					tagsSet.add(folder.getTags()[i]);
				}
			}
			folderVO.setTagsFromWords(tagsSet);

			folder.updateAttributes(folderVO);

			Folder f = folderDao.create(parentFolder, folderVO, true, transaction);

			if (f == null) {
				log.error("Folder {} not created", folderVO.getName());
				throw new Exception("error");
			}

			WSFolder createdFolder = WSFolder.fromFolder(f);
			log.info("Created folder {}", createdFolder.getName());
			return createdFolder;
		} catch (Throwable t) {
			log.error(t.getMessage(), t);
			throw new RuntimeException(t.getMessage(), t);
		}
	}

}