From 6cf26b833e80eba3110b7a7707c2db0bae032129 Mon Sep 17 00:00:00 2001 From: konermann Date: Fri, 9 Oct 2015 11:58:16 +0000 Subject: [PATCH] CCM NG: reworked ShortcutManager git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3683 8810af33-2d31-482b-a856-94f89814c4df --- .../libreccm/shortcuts/ShortcutManager.java | 115 +++++++++++++++--- 1 file changed, 95 insertions(+), 20 deletions(-) diff --git a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutManager.java b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutManager.java index 12104b0e9..2a2b6f723 100644 --- a/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutManager.java +++ b/ccm-shortcuts/src/main/java/org/libreccm/shortcuts/ShortcutManager.java @@ -18,55 +18,130 @@ */ package org.libreccm.shortcuts; +import java.net.URI; +import java.net.URL; +import java.util.Iterator; +import java.util.List; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; /** * This class provides complex operations on {@link Shortcut} objects like * creating a Shortcut. To use this class add an injection point to your class. - * + * * @author Alexander Konermann */ @RequestScoped public class ShortcutManager { /** - * {@link ShortcutRepository} for interacting with the database. The method + * {@link ShortcutRepository} for interacting with the database. */ @Inject private transient ShortcutRepository shortcutRepository; - + /** * Creates a Shortcut - * + * + * @param url + * @param redirect + * @return the new Shortcut */ - private void createShortcut(){ - + public Shortcut createShortcut(final String url, final String redirect) { + Shortcut shortcut = new Shortcut(); + shortcut.setUrlKey(url); + shortcut.setRedirect(redirect); + return shortcut; } - + /** - * Deletes a Shortcut - * + * Creates a Shortcut + * + * @param url + * @param redirect + * @return the new Shortcut */ - private void deleteShortcut(Shortcut shortcut){ - + public Shortcut createShortcut(final URL url, final URL redirect) { + Shortcut shortcut = new Shortcut(); + shortcut.setUrlKey(url.toString()); + shortcut.setRedirect(redirect.toString()); + return shortcut; } - + /** - * checks if the Shortcut exists. + * Creates a Shortcut + * + * @param uri + * @param redirect + * @return the new Shortcut + */ + public Shortcut createShortcut(final URI uri, final URI redirect) { + Shortcut shortcut = new Shortcut(); + shortcut.setUrlKey(uri.toString()); + shortcut.setRedirect(redirect.toString()); + return shortcut; + } + + /** + * checks if the Shortcut exists. * * @return true if the Shortcut exists */ - private boolean testShortcut(Shortcut shortcut){ - return true; + private boolean testShortcut(final Shortcut shortcut) { + return true; } - - + /** * checks if the given URL is valid - * + * + * @param url the Url you want to validate + * @return true if you can succesfully connect to the url, therefore is + * valid. */ - private boolean testURL(String url){ - return true; + private boolean validateURL(final String url) { + //TODO + return false; + + } + + /** + * Finds the first shortcut with the specified urlKey. + * + * @param urlKey the wanted urlKey + * @return Shortcut a shortcut with the specified urlKey + */ + public Shortcut findByUrlKey(final String urlKey) { + //get all Shortcuts: + List shortcutlist = shortcutRepository.findAll(); + //search for the right one: + Iterator iterator = shortcutlist.iterator(); + while (iterator.hasNext()) { + Shortcut shortcut = iterator.next(); + if (shortcut.getUrlKey().equals(urlKey)) { + return shortcut; + } + } + + return null; + } + + /** + * Finds all shortcuts with the specified redirect. + * + * @param redirect the wanted redirect + * @return List a List of Shortcuts with the specified redirect + */ + public List findByRedirect(final String redirect) { + //get all Shortcuts: + List shortcutlist = shortcutRepository.findAll(); + //removes all shortcuts that don't fit + Iterator iterator = shortcutlist.iterator(); + while (iterator.hasNext()) { + Shortcut shortcut = iterator.next(); + if (!shortcut.getRedirect().equals(redirect)) { + shortcutlist.remove(shortcut); + } + } + return shortcutlist; } }