Commit dcb2d26b authored by Piotr Gawron's avatar Piotr Gawron
Browse files

removing unnecessary files

parent 37f14d79
package lcsb.mapviewer.bean;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import lcsb.mapviewer.bean.utils.IPrimefacesUtils;
import lcsb.mapviewer.bean.utils.PrimefacesUtils;
import lcsb.mapviewer.common.MimeType;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.events.Event;
import lcsb.mapviewer.events.Listener;
import lcsb.mapviewer.model.map.model.Model;
/**
* Abstarct bean class containing basic functionality common for all beans
* (property change listeners and vetoable property change listeners).
*
* @author Piotr Gawron
*
*/
public abstract class AbstractManagedBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Util class used to access information about current reqruest and general
* information about P framework.
*/
private transient IPrimefacesUtils primefacesUtils = new PrimefacesUtils();
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(AbstractManagedBean.class);
/**
* Set of verification listeners called when some property is changed. It's
* marked transient because we don't want to store this in session. TODO it's
* should be modified.
*/
private final VetoableChangeSupport vchs = new VetoableChangeSupport(this);
/**
* Set of listeners called when some property is changed. It's marked
* transient because we don't want to store this in session. TODO it's should
* be modified.
*/
private final PropertyChangeSupport pchs = new PropertyChangeSupport(this);
/**
* This map contains information about {@link Listener listeners} that should
* be handled when {@link #callListeners(Event)} method is called. It's marked
* transient because we don't want to store this in session. TODO it's should
* be modified.
*/
private transient Map<Class<? extends Event>, List<Listener<?>>> listeners = new HashMap<Class<? extends Event>, List<Listener<?>>>();
/**
* Default constructor. Adds generic log listener to property change
* listeners.
*/
public AbstractManagedBean() {
// logger property listener
PropertyChangeListener propertyChangeLogger = new PropertyChangeListener() {
@Override
public void propertyChange(final PropertyChangeEvent arg0) {
logger.debug("Property changed: " + arg0.getPropertyName() + ". Old: " + arg0.getOldValue() + " New: " + arg0.getNewValue());
}
};
addPropertyChangeListener(propertyChangeLogger);
}
/**
* Method run after creating all the beans. It's used for data initalization.
*/
public abstract void init();
/**
* Initialize common data and call custom initializer of the bean.
*/
@PostConstruct
public void internalInit() {
createSession();
logger.debug("Initializing bean: " + this);
// call init method for specific bean
init();
logger.debug("Bean: " + this + " initialized.");
}
/**
* Creates http session if session hasn't been created yet.
*/
public void createSession() {
getPrimefacesUtils().createSession();
}
/**
* Add property change listener that can VETO (cancel) the property change.
*
* @param listener
* property listener
*/
public final void addVetoablePropertyChangeListener(final VetoableChangeListener listener) {
vchs.addVetoableChangeListener(listener);
}
/**
* Removes property change l;istener that can VETO from the available
* listeners.
*
* @param listener
* listeren to be removed
*/
public final void removeVetoablePropertyChangeListener(final VetoableChangeListener listener) {
vchs.removeVetoableChangeListener(listener);
}
/**
* Adds property change listener that is fired after property of a bean is
* changed.
*
* @param listener
* listener to be added
*/
public final void addPropertyChangeListener(final PropertyChangeListener listener) {
pchs.addPropertyChangeListener(listener);
}
/**
* Removes property change listener from the list of all available listeners.
*
* @param listener
* listener to be removed
*/
public final void removePropertyChangeListener(final PropertyChangeListener listener) {
pchs.removePropertyChangeListener(listener);
}
/**
* Method that fires all vetoable property change listener for a given
* property.
*
* @param propertyName
* property that has changed
* @param oldValue
* old value of the property
* @param newValue
* new value of the property
* @throws PropertyVetoException
* if the change shouldn't be made this exception is thrown
*/
protected final void fireVetoableChange(final String propertyName, final Object oldValue, final Object newValue) throws PropertyVetoException {
vchs.fireVetoableChange(propertyName, oldValue, newValue);
}
/**
* Method fires property change listener for a given property.
*
* @param propertyName
* name of the property that has changed
* @param oldValue
* old value of the property
* @param newValue
* new value of the property
*/
protected final void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) {
if (oldValue != null || newValue != null) {
pchs.firePropertyChange(propertyName, oldValue, newValue);
}
}
/**
* Returns list of all property change listeners for the bean (includes only
* standard property change listeners, vetoable property change listeners are
* excluded).
*
* @return list of property change listeners
*/
protected final PropertyChangeListener[] getPropertyChangeListeners() {
return pchs.getPropertyChangeListeners();
}
// TODO refactor these two methods to reduce copy-paste
/**
* Method sends to a client response with an attached file. The content of the
* file is taken from the content param. Type defines MimeType of the content
* (text file, xml, etc.) - this helps browser to properly handle response.
*
* @param content
* - string with the file content
* @param fileName
* - name of the file that should popup in the client browser
* @param type
* - MIME type of the file
* @throws IOException
* exception thrown when there are some problems with sending file
*/
protected final void sendFileAsResponse(String content, String fileName, MimeType type) throws IOException {
// send the response to client
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType(type.getTextRepresentation());
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
ec.addResponseHeader("Content-Type", type.getTextRepresentation());
OutputStream output = ec.getResponseOutputStream();
PrintStream printStream = new PrintStream(output);
printStream.print(content);
fc.responseComplete();
}
/**
* Method sends to a client response with an attached file. Type defines
* MimeType of the file content (text file, xml, etc.) - this helps browser to
* properly handle response.
*
* @param file
* - file which will be sent
* @param fileName
* - name of the file that should popup in the client browser
* @param type
* - MIME type of the file
* @throws IOException
* exception thrown when there are some problems with sending file
*/
protected void sendFileAsResponse(File file, String fileName, MimeType type) throws IOException {
// start sending
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType(type.getTextRepresentation());
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// attach file
OutputStream output = ec.getResponseOutputStream();
FileInputStream is = new FileInputStream(file);
IOUtils.copy(is, output);
fc.responseComplete();
}
/**
* Clears whole data shared by this bean.
*/
public abstract void clear();
/**
* Register new {@link Listener} in this bean.
*
* @param listener
* listener that should be added
*/
public void registerListener(Listener<?> listener) {
List<Listener<?>> listenerList = getListeners().get(listener.getEventClass());
if (listenerList == null) {
listenerList = new ArrayList<Listener<?>>();
listeners.put(listener.getEventClass(), listenerList);
}
listenerList.add(listener);
}
/**
* This method will call all listeners associated with the specified
* {@link Event}.
*
* @param event
* {@link Event} for which listeners will be called
*/
protected void callListeners(Event event) {
List<Listener<?>> listenerList = getListeners().get(event.getClass());
if (listenerList == null) {
logger.warn("No listeners found for event: " + event);
} else {
for (Listener<?> listener : listenerList) {
listener.handleEvent(event);
}
}
}
/**
* Unregister {@link Listener} from this bean.
*
* @param listener
* listener that should be removed
*/
public void unregisterListener(Listener<?> listener) {
List<Listener<?>> listenerList = listeners.get(listener.getEventClass());
if (listenerList == null) {
listenerList = new ArrayList<Listener<?>>();
listeners.put(listener.getEventClass(), listenerList);
}
if (listenerList.contains(listener)) {
listenerList.remove(listener);
} else {
throw new InvalidArgumentException("Listener " + listener + " wasn't registered.");
}
}
/**
* @param primefacesUtils
* the primefacesUtils to set
* @see #primefacesUtils
*/
protected void setPrimefacesUtils(IPrimefacesUtils primefacesUtils) {
this.primefacesUtils = primefacesUtils;
}
/**
* Returns value of the parameter from http request call.
*
* @param name
* name of the parameter
* @return value of the request parameter
*/
protected String getRequestParameter(String name) {
return getPrimefacesUtils().getRequestParameter(name);
}
/**
* Adds a parameter that will be stored in user session.
*
* @param key
* name of the parameter
* @param value
* value of the parameter
*/
protected void addSessionParam(String key, Object value) {
getPrimefacesUtils().addSessionParam(key, value);
}
/**
* Gets a value of the parameter stored in user session.
*
* @param key
* name of the parameter
* @return value of the user session parameter
*/
protected Object getSessionParam(String key) {
return getPrimefacesUtils().getSessionParam(key);
}
/**
* @return the version of primefaces library
*/
public String getPrimefacesVersion() {
return getPrimefacesUtils().getVersion();
}
/**
* Returns path on hard drive where the project is deplyed.
*
* @return path where the projest is running
*/
public String getProjectDeployPath() {
return getPrimefacesUtils().getPath();
}
/**
* Sends an error message to the client.
*
* @param message
* error message to be sent
*/
protected void sendError(String message) {
getPrimefacesUtils().error(message);
}
/**
* Sends an error message to the client.
*
* @param message
* error message to be sent
* @param exception
* exception which caused the error
*/
protected void sendError(String message, Exception exception) {
logger.error(message, exception);
getPrimefacesUtils().error(message);
}
/**
* Sends info message to the client.
*
* @param message
* info message to be sent
*/
protected void sendInfo(String message) {
getPrimefacesUtils().info(message);
}
/**
* Returns ip address of current client.
*
* @return IP addresss of the client
*/
protected String getClientIpAddress() {
return getPrimefacesUtils().getClientIpAddress();
}
/**
* Returns a {@link UIComponent} corresponding to the client side component
* with the identifier given in the parameter.
*
* @param id
* identifier of a component
* @return {@link UIComponent} corresponding to the client side component with
* the identifier given in the parameter
*/
protected UIComponent findComponent(String id) {
return getPrimefacesUtils().findComponent(id);
}
/**
* Returns bean identified by {@link Class}.
*
* @param clazz
* class of the bean
* @param <T>
* class of the bean
* @return bean identified by {@link Class}
*/
public <T> T findBean(Class<T> clazz) {
return getPrimefacesUtils().findBean(clazz);
}
/**
* @return primefacesUtils.
*/
public IPrimefacesUtils getPrimefacesUtils() {
if (primefacesUtils == null) {
primefacesUtils = new PrimefacesUtils();
}
return primefacesUtils;
}
/**
* @return listeners.
*/
public Map<Class<? extends Event>, List<Listener<?>>> getListeners() {
if (listeners == null) {
listeners = new HashMap<Class<? extends Event>, List<Listener<?>>>();
}
return listeners;
}
/**
* @param listeners
* general listeners.
*/
public void setListeners(Map<Class<? extends Event>, List<Listener<?>>> listeners) {
this.listeners = listeners;
}
/**
* Executes javascript code on the client browser.
*
* @param javascript
* javascript code
*/
public void executeJavascript(String javascript) {
getPrimefacesUtils().executeJavascript(javascript);
}
/**
* Transform json string representing list of pairs with model and object id
* (it can be list of alias ids or reaction ids) into a {@link List} of
* {@link Pair pairs}.
*
* @param string
* list containing pairs of identifiers
* @return list of pairs with model and alias id: {@link Pair#left} contains
* {@link Model#getId()} and {@link Pair#right} conatins
* {@link lcsb.mapviewer.model.map.species.Element#id}.
*
*/
protected List<Pair<Integer, Integer>> deserializeJsonIds(String string) {
List<Pair<Integer, Integer>> result = new ArrayList<>();
@SuppressWarnings("unchecked")
List<List<?>> list = new Gson().fromJson(string, List.class);
for (List<?> list2 : list) {
Integer left = null;
Object leftObj = list2.get(0);
if (leftObj instanceof Double) {
left = ((Double) leftObj).intValue();
} else if (leftObj instanceof String) {
left = Integer.valueOf((String) leftObj);
} else {
throw new InvalidArgumentException("Don't know how to handle class: " + leftObj.getClass());
}
Integer right = null;
Object rightObj = list2.get(1);
if (rightObj instanceof Double) {
right = ((Double) rightObj).intValue();
} else if (rightObj instanceof String) {
right = Integer.valueOf((String) rightObj);
} else {
throw new InvalidArgumentException("Don't know how to handle class: " + rightObj.getClass());
}
result.add(new Pair<Integer, Integer>(left, right));
}
return result;
}
}
package lcsb.mapviewer.bean;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import lcsb.mapviewer.common.Pair;
import lcsb.mapviewer.services.search.ElementIdentifierDetails;
import lcsb.mapviewer.services.search.ISearchResultView;
import lcsb.mapviewer.services.search.data.ElementIdentifier;
import lcsb.mapviewer.services.search.data.ElementIdentifier.ElementIdentifierType;
/**
* Abstract bean class containing interface to connect with google maps API on
* the client side.
*
* @param <T>
* type of search result returned by single query
*
* @author Piotr Gawron
*
*/
public abstract class AbstractMarkerManagerBean<T extends ISearchResultView> extends AbstractManagedBean {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Default class logger.
*/
private static Logger logger = Logger.getLogger(AbstractMarkerManagerBean.class);
/**
* Object with all results to be send to the client.
*/
private List<T> results = new ArrayList<>();
/**
* List of elements that should be visualized on the map. Any element in
* {@link #results} can be visualized at many places (or none at all) o the