package ch.ffhs.webE.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.StrutsStatics;
import ch.ffhs.webE.dao.HistoryDAO;
import ch.ffhs.webE.dao.RelationshipDAO;
import ch.ffhs.webE.dao.RelationshipTypeDAO;
import ch.ffhs.webE.dao.TermDAO;
import ch.ffhs.webE.dao.UserDAO;
import ch.ffhs.webE.domain.ActionType;
import ch.ffhs.webE.domain.History;
import ch.ffhs.webE.domain.ObjectEntity;
import ch.ffhs.webE.domain.ObjectType;
import ch.ffhs.webE.domain.Relationship;
import ch.ffhs.webE.domain.RelationshipType;
import ch.ffhs.webE.domain.Term;
import ch.ffhs.webE.domain.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* Implements actions applicable to relationship editing
*
* @author Thomas Lahn
*/
public class RelationshipAction
extends ActionSupport
implements
ModelDriven
<Relationship
>
{
private static final long serialVersionUID = 1L
;
private List<RelationshipType
> relationshipTypes =
new ArrayList<RelationshipType
>();
private final RelationshipTypeDAO relationshipTypeDAO =
new RelationshipTypeDAO
();
private List<Term
> terms =
new ArrayList<Term
>();
private final TermDAO termDAO =
new TermDAO
();
private List<Relationship
> relationshipList =
new ArrayList<Relationship
>();
private Relationship relationship =
new Relationship
();
/**
* The term that was just saved (added, renamed)
*/
private Relationship modifiedRelationship
;
private final RelationshipDAO relationshipDAO =
new RelationshipDAO
();
private final UserDAO userDAO =
new UserDAO
();
private Set<History
> history
;
private final HistoryDAO historyDAO =
new HistoryDAO
();
/**
* Session object
*/
Map<String,
Object> session = ActionContext.
getContext().
getSession();
/**
* @var <code>true</code> if the relationship is to be edited/renamed,
* <code>false</code> otherwise
*/
public boolean edit =
false;
/**
* @var <code>true</code> if a relationship was added, <code>false</code>
* otherwise
*/
public boolean added =
false;
private final HttpServletRequest request =
(HttpServletRequest
) ActionContext
.
getContext().
get(StrutsStatics.
HTTP_REQUEST);
/*
* (non-Javadoc)
*
* @see com.opensymphony.xwork2.ModelDriven#getModel()
*/
public Relationship getModel
()
{
return this.
relationship;
}
/**
* DB query for relationship list
*
* @return SUCCESS
*/
public String list
()
{
this.
setTerms(this.
termDAO.
getList());
this.
setRelationshipTypes(this.
relationshipTypeDAO.
getList());
this.
setRelationshipList(this.
relationshipDAO.
getList());
return Action.
SUCCESS;
}
/**
* Executes the DB query to save the relationship
*
* @return {@link Action#SUCCESS}
*/
public String save
()
{
this.
relationship.
setTermFrom(this.
termDAO.
getById(Integer
.
parseInt(this.
request.
getParameter("term1"))));
this.
relationship.
setTermTo(this.
termDAO.
getById(Integer
.
parseInt(this.
request.
getParameter("term2"))));
this.
relationship.
setRelationshipType(this.
relationshipTypeDAO
.
getById(Integer.
parseInt(this.
request.
getParameter("type"))));
User user =
this.
userDAO.
getByUsername((String) this.
session
.
get("username"));
Date now =
new Date();
ObjectEntity obj
;
int action =
0;
if ("false".
equals(this.
request.
getParameter("edit")))
{
/* Add a new relationship */
obj =
new ObjectEntity
(user,
new ObjectType
(ObjectType.
RELATIONSHIP),
user,
null,
new Date(),
false,
null,
null,
this.
relationship);
this.
relationship.
setObject(obj
);
this.
added =
true;
action = ActionType.
ADD;
}
else
{
obj =
new ObjectEntity
();
obj.
setId(this.
relationship.
getObjectId());
action = ActionType.
MODIFY;
}
this.
edit =
false;
String result =
Action.
SUCCESS;
if (this.
relationshipDAO.
saveOrUpdate(this.
relationship))
{
String comment =
this.
request.
getParameter("comment");
History historyRecord =
new History
(user,
new ActionType
(action
), obj,
"(" +
this.
relationship.
getTermFrom().
getName() +
") ("
+
this.
relationship.
getRelationshipType().
getNameFrom() +
") ("
+
this.
relationship.
getTermTo().
getName() +
")", comment, now
);
this.
historyDAO.
saveOrUpdate(historyRecord
);
}
else
{
result =
Action.
ERROR;
}
this.
setModifiedRelationship(this.
relationship);
this.
setRelationship(null);
this.
list();
return result
;
}
/**
* @return {@link Action#SUCCESS} if <var>id</var> > 0, {@link Action#ERROR}
* otherwise
*/
public String edit
()
{
int id =
this.
getIdParameter();
String result =
Action.
ERROR;
if (id
> 0)
{
this.
setRelationship(this.
relationshipDAO.
getById(id
));
if (this.
getRelationship() !=
null)
{
this.
edit =
true;
result =
Action.
SUCCESS;
}
}
this.
list();
return result
;
}
/**
* deletes a user, gets the ID from the "id" parameter that was submitted with
* the HTTP request
*
* @return String - either SUCCESS or ERROR constant
*/
public String delete
()
{
int id =
this.
getIdParameter();
/* Check for malicious ID values */
String result =
Action.
SUCCESS;
if (id
> 0)
{
this.
relationshipDAO.
delete(id
);
}
else
{
result =
Action.
ERROR;
}
this.
list();
return result
;
}
/**
* Gets the ID Parameter for update / delete requests
*
* @return int from the ID request. If not set or wrong, it gives back -1
*/
private int getIdParameter
()
{
int id = -
1;
try
{
id =
Integer.
parseInt(this.
request.
getParameter("id")); //$NON-NLS-1$
}
catch (Exception e
)
{
/* TODO: Logging - wrong parameter set */
}
return id
;
}
/* Standard getters and setters */
/**
* @return The relationship edited with this instance
*/
public Relationship getRelationship
()
{
return this.
relationship;
}
/**
* @param relationship
* The relationship edited with this instance
*/
public void setRelationship
(Relationship relationship
)
{
this.
relationship = relationship
;
}
/**
* @return The list of terms edited with this instance
*/
public List<Relationship
> getRelationshipList
()
{
return this.
relationshipList;
}
/**
* @param relationshipList
* The list of terms edited with this instance
*/
public void setRelationshipList
(List<Relationship
> relationshipList
)
{
this.
relationshipList = relationshipList
;
}
/**
* @return the relationshipTypes
*/
public List<RelationshipType
> getRelationshipTypes
()
{
return this.
relationshipTypes;
}
/**
* @param relationshipTypes
* the relationshipTypes to set
*/
public void setRelationshipTypes
(List<RelationshipType
> relationshipTypes
)
{
this.
relationshipTypes = relationshipTypes
;
}
/**
* @return the terms
*/
public List<Term
> getTerms
()
{
return this.
terms;
}
/**
* @param terms
* the terms to set
*/
public void setTerms
(List<Term
> terms
)
{
this.
terms = terms
;
}
/**
* @return the modifiedRelationship
*/
public Relationship getModifiedRelationship
()
{
return this.
modifiedRelationship;
}
/**
* @param modifiedRelationship
* the modifiedRelationship to set
*/
public void setModifiedRelationship
(Relationship modifiedRelationship
)
{
this.
modifiedRelationship = modifiedRelationship
;
}
/**
* @return the history
*/
public Set<History
> getHistory
()
{
return this.
history;
}
/**
* @param history
* the history to set
*/
public void setHistory
(Set<History
> history
)
{
this.
history = history
;
}
}