package healthwatcher.view.command;
import healthwatcher.Constants;
import healthwatcher.model.healthguide.HealthUnit;
import healthwatcher.view.IFacade;
import java.io.PrintWriter;
import lib.exceptions.CommunicationException;
import lib.exceptions.FacadeUnavailableException;
import lib.exceptions.InvalidSessionException;
import lib.exceptions.ObjectNotFoundException;
import lib.exceptions.RepositoryException;
import lib.exceptions.TransactionException;
import lib.util.HTMLCode;
public class UpdateHealthUnitSearch extends Command {
public UpdateHealthUnitSearch(IFacade f) {
super(f);
// TODO Auto-generated constructor stub
}
public static final String HEALTH_UNIT = "health unit";
public void execute() throws Exception {
PrintWriter out = response.getWriter();
try {
if (! request.isAuthorized()) {
throw new InvalidSessionException();
}
Long numUS = (new Long(request.getInput("numUS"))).longValue();
HealthUnit unit = facade.searchHealthUnit(numUS);
//#if relacional
unit.addObserver(facade); //thiago alterou aqui
//#endif
request.put(UpdateHealthUnitSearch.HEALTH_UNIT, unit);
out.println("Update Health Unit");
out.println("<script language=\"javascript\">");
out.println("function submeterDados(modulo)");
out.println("{");
String a1 = "\"";
String a2 = "\"";
out.println(" var f = document.formAlterarUnidade2;");
out.println(" if(f.descricao.value ==" + a1 + a2 + ")");
out.println(" {");
out.println(" alert(\"Digite a nova descri��o da unidade!\");");
out.println(" f.descricao.select();");
out.println(" return;");
out.println(" }");
out.println(" f.submit();");
out.println("}");
out.println("//--></script>");
out.println("<body><h1>Update Health unit:</h1>");
out.println("<form method=\"POST\" name=\"formAlterarUnidade2\" action=\""+Constants.SYSTEM_ACTION+"?operation=UpdateHealthUnitData\">");
out.println("<div align=\"center\"><center><h4>Unit: " + numUS + "</h4></center></div>");
out.println("<div align=\"center\"><center><p><strong>Name:</strong><br><input type=\"text\" name=\"descricao\" value=\"" + unit.getDescription() + "\" size=\"60\"></p></center></div>");
out.println("<div align=\"center\"><center><h4><input type=\"button\" value=\"Update\" name=\"bt1\" onClick=\"javascript:submeterDados();\"><input type=\"reset\" value=\"Clear\" name=\"bt2\"></h4></center></div></form>");
out.println(HTMLCode.closeAdministrator());
} catch (CommunicationException e) {
throw new FacadeUnavailableException();
} catch (ObjectNotFoundException e) {
out.println("Health unit does not exist!");
} catch (InvalidSessionException e) {
out.println(e.getMessage());
}catch (RepositoryException e) {
e.printStackTrace();
} catch(TransactionException e){
e.printStackTrace(out);
} finally {out.close();}
}
}