package pt.estgp.es.exemplos.hibernate.services;
import org.apache.log4j.Logger;
import pt.estgp.es.exemplos.hibernate.*;
import pt.estgp.es.exemplos.hibernate.security.SecurityContextProvider;
import pt.estgp.es.exemplos.hibernate.security.logic.HasRole;
import pt.estgp.es.exemplos.hibernate.security.logic.IsAuthenticated;
import pt.estgp.es.exemplos.hibernate.security.logic.PrintParameterExample;
import pt.estgp.es.exemplos.hibernate.security.logic.Transaction;
import pt.estgp.es.exemplos.hibernate.web.PaginasServlet;
import pt.estgp.es.exemplos.hibernate.web.SeccaoServlet;
import javax.servlet.ServletException;
public class ServicoPaginas
{
private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger =
1.5.0/docs/api/java/util/logging/Logger.html">Logger.
getLogger(ServicoPaginas.
class);
@Transaction @IsAuthenticated @HasRole
(role =
"admin") @PrintParameterExample
(paramName =
"idPagina")
public void criaSeccaoService
(1.5.0/docs/api/java/lang/String.html">String idPagina,
1.5.0/docs/api/java/lang/String.html">String titulo
)
{
try {
new PaginasServlet
().
init();
} catch (ServletException e
) {
e.
printStackTrace();
}
Pagina p =
(Pagina
) HibernateUtils.
getCurrentSession().
load(Pagina.
class,
new 1.5.0/docs/api/java/lang/Long.html">Long(idPagina
));
Seccao seccao =
new SeccaoImpl
();
seccao.
setTitulo(titulo
);
p.
getSeccoes().
add(seccao
);
seccao.
setPagina(p
);
HibernateUtils.
getCurrentSession().
save(seccao
);
}
@Transaction @IsAuthenticated
public void criaComponenteTextoService
(1.5.0/docs/api/java/lang/String.html">String idSeccao,
1.5.0/docs/api/java/lang/String.html">String texto,
1.5.0/docs/api/java/lang/String.html">String titulo
)
{
try {
new SeccaoServlet
().
init();
} catch (ServletException e
) {
e.
printStackTrace();
}
Seccao seccao =
(Seccao
) HibernateUtils.
getCurrentSession().
load(Seccao.
class,
new 1.5.0/docs/api/java/lang/Long.html">Long(idSeccao
));
long id = seccao.
getPagina().
getId();
ConteudoTexto conteudoTexto=
new ConteudoTextoImpl
();
conteudoTexto.
setTitulo(titulo
);
conteudoTexto.
setTexto(texto
);
seccao.
getConteudoTexto().
add(conteudoTexto
);
conteudoTexto.
setSeccao(seccao
);
HibernateUtils.
getCurrentSession().
save(conteudoTexto
);
}
@Transaction @IsAuthenticated @HasRole
(role =
"admin")
public long deleteSeccaoService
(1.5.0/docs/api/java/lang/String.html">String idSeccao
)
{
Seccao seccao =
(Seccao
) HibernateUtils.
getCurrentSession().
load(Seccao.
class,
new 1.5.0/docs/api/java/lang/Long.html">Long(idSeccao
));
long id = seccao.
getPagina().
getId();
seccao.
getPagina().
getSeccoes().
remove(seccao
);
seccao.
setPagina(null);
HibernateUtils.
getCurrentSession().
delete(seccao
);
return id
;
}
@Transaction @IsAuthenticated
public long deleteConteudoTextoService
(1.5.0/docs/api/java/lang/String.html">String idConteudoTexto
) {
ConteudoTexto conteudoTexto =
(ConteudoTexto
) HibernateUtils.
getCurrentSession().
load(ConteudoTexto.
class,
new 1.5.0/docs/api/java/lang/Long.html">Long(idConteudoTexto
));
long id = conteudoTexto.
getSeccao().
getId();
conteudoTexto.
getSeccao().
getConteudoTexto().
remove(conteudoTexto
);
conteudoTexto.
setSeccao(null);
HibernateUtils.
getCurrentSession().
delete(conteudoTexto
);
return id
;
}
@Transaction @IsAuthenticated
public Pagina criaPaginaService
(1.5.0/docs/api/java/lang/String.html">String titulo
)
{
1.5.0/docs/api/java/lang/String.html">String cookie = SecurityContextProvider.
getInstance().
getSecuritySessionContext().
getRequester();
UserSession session =
(UserSession
) HibernateUtils.
getCurrentSession().
load(UserSession.
class,cookie
);
Pagina p =
new PaginaImpl
();
p.
setDono(session.
getUser());
p.
setNome(titulo
);
HibernateUtils.
getCurrentSession().
save(p
);
return p
;
}
}