CCM NG: reworked ShortcutManager

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3683 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
konermann 2015-10-09 11:58:16 +00:00
parent dad4ce0c53
commit 6cf26b833e
1 changed files with 95 additions and 20 deletions

View File

@ -18,6 +18,10 @@
*/ */
package org.libreccm.shortcuts; 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.enterprise.context.RequestScoped;
import javax.inject.Inject; import javax.inject.Inject;
@ -31,7 +35,7 @@ import javax.inject.Inject;
public class ShortcutManager { public class ShortcutManager {
/** /**
* {@link ShortcutRepository} for interacting with the database. The method * {@link ShortcutRepository} for interacting with the database.
*/ */
@Inject @Inject
private transient ShortcutRepository shortcutRepository; private transient ShortcutRepository shortcutRepository;
@ -39,17 +43,43 @@ public class ShortcutManager {
/** /**
* Creates a Shortcut * 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;
}
/**
* 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;
} }
/** /**
@ -57,16 +87,61 @@ public class ShortcutManager {
* *
* @return true if the Shortcut exists * @return true if the Shortcut exists
*/ */
private boolean testShortcut(Shortcut shortcut){ private boolean testShortcut(final Shortcut shortcut) {
return true; return true;
} }
/** /**
* checks if the given URL is valid * 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){ private boolean validateURL(final String url) {
return true; //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<Shortcut> 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<Shortcut> a List of Shortcuts with the specified redirect
*/
public List<Shortcut> findByRedirect(final String redirect) {
//get all Shortcuts:
List shortcutlist = shortcutRepository.findAll();
//removes all shortcuts that don't fit
Iterator<Shortcut> iterator = shortcutlist.iterator();
while (iterator.hasNext()) {
Shortcut shortcut = iterator.next();
if (!shortcut.getRedirect().equals(redirect)) {
shortcutlist.remove(shortcut);
}
}
return shortcutlist;
} }
} }