📄 applicationhandler.java
字号:
/** * */package com.pjaol.ifodder.application;import java.util.Map;import java.util.StringTokenizer;import java.util.Vector;import org.apache.log4j.Logger;import com.pjaol.ifodder.storage.IRetrival;import com.pjaol.ifodder.storage.IStorage;import com.pjaol.ifodder.storage.ListItem;import com.pjaol.ifodder.storage.StorageException;/** * @author pjaol * */public class ApplicationHandler { private enum actions {addListItem, addTag, getListItem, getListItemsByUser} ; private static IStorage store; private static IRetrival retrive; private Logger _log = Logger.getLogger(getClass()); public String handle(String action, Map arguments) throws StorageException{ switch (actions.valueOf(action)) { case addListItem: addListItem(arguments); return "ok"; case addTag: addTag(arguments); return "ok"; case getListItem: return getListItem(arguments); case getListItemsByUser: return getListItemsByUser(arguments); } return errorString("handler: no valid action"); } private void addListItem (Map arguments) throws StorageException { String url = (String)arguments.get("url"); int user_id = ((Integer) arguments.get("user_id")).intValue(); String description = (String) arguments.get("description"); String tags = (String) arguments.get("tags"); int datum_id = -1; try { datum_id = store.getDatumId(url); } catch (StorageException e) { throw new StorageException(2,getClass().getName(),"addListItem", "get a datum", e); } if (datum_id < 0) { try { store.addDatum(url); datum_id = store.getDatumId(url); } catch (StorageException e) { throw new StorageException(2,getClass().getName(),"addListItem", "Failed to store datum", e); } } _log.debug("getDatumId returned "+ datum_id); store.addListItem(user_id, datum_id, description); addTag(user_id, datum_id, tags); } private void addTag (Map arguments) throws StorageException{ int user_id = ((Integer)arguments.get("user_id")).intValue(); String tags = (String) arguments.get("tags"); int datum_id = ((Integer) arguments.get("datum_id")).intValue(); addTag(user_id, datum_id, tags); } private void addTag (int user_id, int datum_id, String tags) throws StorageException { if (tags == null) return; int li_id; li_id = store.getListId(user_id, datum_id); StringTokenizer tokenizer = new StringTokenizer(tags, ",\"", true); while (tokenizer.hasMoreTokens()) { String token = com.pjaol.ifodder.storage.StringUtils.getNextToken(tokenizer); store.addTag(user_id, li_id, token); } } private String getListItem(Map arguments) throws StorageException{ int user_id= ((Integer) arguments.get("user_id")).intValue(); int li_id = ((Integer) arguments.get("li_id")).intValue(); return getListItem(user_id, li_id); } private String getListItem( int user_id, int li_id) throws StorageException { ListItem listItem = retrive.getListItem(user_id, li_id); if (listItem == null) return errorString("getListItem: no such list item"); return listItem.asXML(); } private String getListItemsByUser (Map arguments) throws StorageException{ int user_id= ((Integer) arguments.get("user_id")).intValue(); return getListItemsByUser(user_id); } private String getListItemsByUser (int user_id) throws StorageException{ Vector listItems = retrive.getListItemsByUser(user_id); String result = new String(); int size = listItems.size(); for (int x = 0 ; x < size; x++){ ListItem li = (ListItem) listItems.get(x); result+= li.asXML(); } return result; } private String errorString(String message){ return "<error>"+message+"</error>"; } public void setStorage (String storageClass) throws InstantiationException, IllegalAccessException, ClassNotFoundException { store = (IStorage ) Class.forName(storageClass).newInstance(); } public void setRetrival (String retrivalClass) throws InstantiationException, IllegalAccessException, ClassNotFoundException { retrive = (IRetrival) Class.forName(retrivalClass).newInstance(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -