class FolderServiceImpl extends RemoteServiceServlet implements FolderService {

	@Override
	public GUIFolder save(GUIFolder folder) throws ServerException {
		Session session = ServiceUtil.validateSession(getThreadLocalRequest());
		
		FolderDAO folderDao = (FolderDAO) Context.get().getBean(FolderDAO.class);
		try {
			Folder f = folderDao.findById(folder.getId());
			folderDao.initialize(f);

			FolderHistory transaction = new FolderHistory();
			transaction.setSession(session);

			String folderName = folder.getName().replace("/", "");

			if (folder.getFoldRef() != null) {
				// The user is editing an alias
				Folder alias = folderDao.findById(folder.getFoldRef());
				folderDao.initialize(alias);
				alias.setDescription(folder.getDescription());
				alias.setPosition(folder.getPosition());
				alias.setName(folderName);
				folderDao.store(alias);
			} else {
				// The user is editing a real folder
				f.setDescription(folder.getDescription());
				f.setPosition(folder.getPosition());
				f.setType(folder.getType());

				if (f.isWorkspace()) {
					f.setStorage(folder.getStorage());
					f.setMaxVersions(folder.getMaxVersions());
				}

				if (f.getName().trim().equals(folderName)) {
					f.setName(folderName.trim());
					transaction.setEvent(FolderEvent.CHANGED.toString());
				} else {
					f.setName(folderName.trim());
					transaction.setEvent(FolderEvent.RENAMED.toString());
				}
			}

			f.setTemplateLocked(folder.getTemplateLocked());
			f.setQuotaDocs(folder.getQuotaDocs());
			f.setQuotaSize(folder.getQuotaSize());
			f.setQuotaThreshold(folder.getQuotaThreshold());
			f.setQuotaAlertRecipients(folder.getQuotaAlertRecipientsAsString());
			f.setColor(folder.getColor());
			f.setGrid(folder.getGrid());
			f.setOcrTemplateId(folder.getOcrTemplateId());
			f.setBarcodeTemplateId(folder.getBarcodeTemplateId());

			updateExtendedAttributes(f, folder);

			if (folder.getTags() != null && folder.getTags().length > 0)
				f.setTagsFromWords(new HashSet<String>(Arrays.asList(folder.getTags())));
			else
				f.getTags().clear();

			folderDao.store(f, transaction);
		} catch (Throwable t) {
			ServiceUtil.throwServerException(session, log, t);
		}

		return getFolder(session, folder.getId());
	}

}