package pt.estgp.estgweb.services.directories;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import pt.estgp.estgweb.utils.Globals;
import pt.estgp.estgweb.domain.ConfigurationDirectory;
import pt.estgp.estgweb.domain.ConfigurationDirectoryImpl;
import pt.estgp.estgweb.domain.DomainObjectFactory;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.filters.exceptions.NotFoundException;
import pt.estgp.estgweb.services.directories.xsd.*;
import pt.estgp.estgweb.services.expceptions.AlreadyExistsException;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.utils.Dom4jUtil;
import pt.utl.ist.berserk.logic.serviceManager.IService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
/**
* @author Jorge Machado
*/
public class DirectoryService
implements IService
{
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(DirectoryService.
class);
/*
public List<DirectoryImpl> loadDirectories(String classesPath, String place)
{
List<DirectoryImpl> directories = new ArrayList<DirectoryImpl>();
String directoryResources = classesPath + "/pt/estgp/estgweb/directories";
if (directoryResources.startsWith("file:"))
{
directoryResources = directoryResources.substring("file:".length());
}
System.out.println("Using directory resources from:" + directoryResources);
File f = new File(directoryResources);
for (File dir : f.listFiles())
{
if (dir.getName().endsWith("xml"))
{
logger.info("Possible directory: " + dir.getName());
try
{
Document directoryDocument = Dom4jUtil.parse(dir);
String targetPlace = directoryDocument.getRootElement().attribute("place").getValue();
if (targetPlace.equals(place))
{
String targetRole = directoryDocument.getRootElement().attribute("targetRole").getValue();
String id = directoryDocument.getRootElement().attribute("id").getValue();
String menuKey = directoryDocument.getRootElement().attribute("menuKey").getValue();
DirectoryImpl directory = new DirectoryImpl();
directory.setId(id);
directory.setKey(menuKey);
directory.setRole(targetRole);
directories.add(directory);
}
}
catch (DocumentException e)
{
logger.error("Error parsing directory " + dir.getAbsolutePath(), e);
}
catch (MalformedURLException e)
{
logger.error("Error parsing directory " + dir.getAbsolutePath(), e);
}
catch (Exception e)
{
logger.error("Error parsing directory " + dir.getAbsolutePath(), e);
}
}
}
return directories;
}
*/
public Directory loadDirectory
(1.5.0/docs/api/java/lang/String.html">String identifier, UserSession sess
)
{
return loadDirectoryConfiguration
(identifier,sess
).
getDirectory();
}
public ConfigurationDirectoryImpl loadDirectoryConfiguration
(1.5.0/docs/api/java/lang/String.html">String identifier, UserSession sess
)
{
return loadDirectoryConfiguration
(identifier,
false, sess
);
}
public ConfigurationDirectoryImpl loadDirectoryConfigurationCached
(1.5.0/docs/api/java/lang/String.html">String identifier, UserSession sess
)
{
return loadDirectoryConfiguration
(identifier,
true, sess
);
}
private static Map
<String,ConfigurationDirectoryImpl
> cache =
new HashMap
<String, ConfigurationDirectoryImpl
>();
/**
* The identifier used as key is the String dirId of ConfigurationDirectory only
* So this service only works with String keys
* Administration services never use the cache
* @param identifier dirId
* @param toPut if filled will put
* @return the affected object
*/
private static synchronized ConfigurationDirectoryImpl getPutCache
(1.5.0/docs/api/java/lang/String.html">String identifier,ConfigurationDirectoryImpl toPut
)
{
if(identifier
!=
null)
return cache.
get(identifier
);
if(toPut
!=
null)
cache.
put(toPut.
getIdDir(),toPut
);
return toPut
;
}
private ConfigurationDirectoryImpl loadDirectoryConfiguration
(1.5.0/docs/api/java/lang/String.html">String identifier,
boolean useCache, UserSession sess
)
{
if(identifier.
equals("new"))
{
ConfigurationDirectoryImpl dir = DomainObjectFactory.
createConfigurationDirectoryImpl();
dir.
setIdDir("new"+
1.5.0/docs/api/java/lang/System.html">System.
currentTimeMillis());
identifier =
"new"+
1.5.0/docs/api/java/lang/System.html">System.
currentTimeMillis();
dir.
setFixedIdDir(false);
try {
JAXBContext jc = JAXBContext.
newInstance(Directory.
class);
Unmarshaller unmarshaller = jc.
createUnmarshaller();
Directory directory =
(Directory
) unmarshaller.
unmarshal(DirectoryService.
class.
getResourceAsStream("/pt/estgp/estgweb/directories/newEmptyDir.xml"));
Marshaller marshaller = jc.
createMarshaller();
marshaller.
setProperty(Marshaller.
JAXB_FORMATTED_OUTPUT,
true);
1.5.0/docs/api/java/io/StringWriter.html">StringWriter sw =
new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter();
marshaller.
marshal(directory, sw
);
dir.
setXml(sw.
toString());
DaoFactory.
getConfigurationDirectoryDaoImpl().
save(dir
);
} catch (JAXBException e
) {
logger.
error(e,e
);
}
}
if(useCache
)
{
ConfigurationDirectoryImpl cached = getPutCache
(identifier,
null);
if(cached
!=
null)
{
return cached
;
}
}
Directory directory =
null;
ConfigurationDirectoryImpl dir = DaoFactory.
getConfigurationDirectoryDaoImpl().
loadDirectory(identifier
);
JAXBContext jc
;
if(dir ==
null)
{
try
{
jc = JAXBContext.
newInstance(Directory.
class);
Unmarshaller unmarshaller = jc.
createUnmarshaller();
directory =
(Directory
) unmarshaller.
unmarshal(DirectoryService.
class.
getResourceAsStream("/pt/estgp/estgweb/directories/" + identifier +
".xml"));
dir = DomainObjectFactory.
createConfigurationDirectoryImpl();
dir.
setDescricao("");
dir.
setIdDir(identifier
);
Marshaller marshaller = jc.
createMarshaller();
marshaller.
setProperty(Marshaller.
JAXB_FORMATTED_OUTPUT,
true);
1.5.0/docs/api/java/io/StringWriter.html">StringWriter sw =
new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter();
marshaller.
marshal(directory, sw
);
dir.
setXml(sw.
toString());
DaoFactory.
getConfigurationDirectoryDaoImpl().
save(dir
);
} catch (JAXBException e
) {
logger.
error(e,e
);
return null;
}
}
else
{
try
{
jc = JAXBContext.
newInstance(Directory.
class);
Unmarshaller unmarshaller = jc.
createUnmarshaller();
unmarshaller.
setProperty("com.sun.xml.bind.ObjectFactory",
new ObjectFactoryImpl
());
1.5.0/docs/api/java/io/StringReader.html">StringReader r =
new 1.5.0/docs/api/java/io/StringReader.html">StringReader(dir.
getXml());
directory =
(Directory
) unmarshaller.
unmarshal(r
);
} catch (JAXBException e
) {
logger.
error(e,e
);
return null;
}
}
dir.
setDirectory(directory
);
checkFixedDirectory
(directory,dir
);
getPutCache
(null,dir
);
return dir
;
}
private void checkFixedDirectory
(Directory directory,ConfigurationDirectory dir
)
{
if(directory.
getId().
equals("topMenus") || directory.
getId().
equals("header") || directory.
getId().
equals("footer") || directory.
getId().
equals("utilsIntranet") )
{
directory.
setFixedDirectoryId(true);
dir.
setFixedIdDir(true);
}
}
public 1.5.0/docs/api/java/lang/String.html">String loadDirectoryXml
(1.5.0/docs/api/java/lang/String.html">String identifier, UserSession sess
)
{
ConfigurationDirectoryImpl dir = DaoFactory.
getConfigurationDirectoryDaoImpl().
loadDirectory(identifier
);
JAXBContext jc
;
if(dir ==
null)
{
try
{
jc = JAXBContext.
newInstance(Directory.
class);
Unmarshaller unmarshaller = jc.
createUnmarshaller();
Directory directory =
(Directory
) unmarshaller.
unmarshal(DirectoryService.
class.
getResourceAsStream("/pt/estgp/estgweb/directories/" + identifier +
".xml"));
dir = DomainObjectFactory.
createConfigurationDirectoryImpl();
checkFixedDirectory
(directory,dir
);
dir.
setDescricao("");
dir.
setIdDir(identifier
);
Marshaller marshaller = jc.
createMarshaller();
marshaller.
setProperty(Marshaller.
JAXB_FORMATTED_OUTPUT,
true);
1.5.0/docs/api/java/io/StringWriter.html">StringWriter sw =
new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter();
marshaller.
marshal(directory, sw
);
dir.
setXml(sw.
toString());
DaoFactory.
getConfigurationDirectoryDaoImpl().
save(dir
);
return sw.
toString();
} catch (JAXBException e
) {
logger.
error(e,e
);
}
}
else
{
return dir.
getXml();
}
return null;
}
private static Map
<String,String
> cachedHtml =
new HashMap
<String, String
>();
private static synchronized 1.5.0/docs/api/java/lang/String.html">String getOrAddOrRemove
(1.5.0/docs/api/java/lang/String.html">String id,
1.5.0/docs/api/java/lang/String.html">String html,
boolean remove
)
{
if(remove
)
cachedHtml.
remove(id
);
else if(html
!=
null)
cachedHtml.
put(id,html
);
else
return cachedHtml.
get(id
);
return null;
}
public 1.5.0/docs/api/java/lang/String.html">String loadDirectoryHtml
(1.5.0/docs/api/java/lang/String.html">String identifier, Map
<String,Object
> parameters, UserSession sess
) throws ServiceException
{
1.5.0/docs/api/java/lang/String.html">String html = Globals.
USE_XSL_CACHE ? getOrAddOrRemove
(identifier,
null,
false) :
null;
if(html
!=
null)
return html
;
1.5.0/docs/api/java/lang/String.html">String xml = loadDirectoryXml
(identifier,sess
);
try
{
5+0%2Fdocs%2Fapi+Document">Document dom = Dom4jUtil.
parse(xml
);
JAXBContext jc = JAXBContext.
newInstance(Directory.
class);
Unmarshaller unmarshaller = jc.
createUnmarshaller();
1.5.0/docs/api/java/io/StringReader.html">StringReader r =
new 1.5.0/docs/api/java/io/StringReader.html">StringReader(xml
);
Directory directory =
(Directory
) unmarshaller.
unmarshal(r
);
html = Dom4jUtil.
styleDocument(dom,
"/pt/estgp/estgweb/directories/" + directory.
getStyle(), parameters,
true);
getOrAddOrRemove
(identifier,html,
false);
} catch (JAXBException e
) {
logger.
error(e,e
);
throw new ServiceException
("Cant load Menu");
} catch (DocumentException e
) {
logger.
error(e, e
);
throw new ServiceException
("Cant load Menu");
} catch (1.5.0/docs/api/java/lang/Exception.html">Exception e
) {
logger.
error(e, e
);
throw new ServiceException
("Cant load Menu");
}
return html
;
}
public void saveDirectory
(Directory directory,
1.5.0/docs/api/java/lang/Long.html">Long dbId, UserSession sess
) throws NotFoundException, ServiceException
{
ConfigurationDirectoryImpl dir = DaoFactory.
getConfigurationDirectoryDaoImpl().
load(dbId
);
if(dir
!=
null)
{
ConfigurationDirectory actualDirId = DaoFactory.
getConfigurationDirectoryDaoImpl().
loadDirectoryDirId(directory.
getId());
if(actualDirId
!=
null && actualDirId.
getId() != dir.
getId())
{
throw new AlreadyExistsException
("Ja existe um directorio com o id " + directory.
getId());
}
checkFixedDirectory
(directory,dir
);
dir.
setIdDir(directory.
getId());
JAXBContext jc =
null;
try {
jc = JAXBContext.
newInstance(Directory.
class);
Marshaller marshaller = jc.
createMarshaller();
marshaller.
setProperty(Marshaller.
JAXB_FORMATTED_OUTPUT,
true);
1.5.0/docs/api/java/io/StringWriter.html">StringWriter sw =
new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter();
marshaller.
marshal(directory, sw
);
dir.
setXml(sw.
toString());
getOrAddOrRemove
(directory.
getId(),
null,
true);
dir.
setDirectory(directory
);
getPutCache
(null, dir
);
}
catch (JAXBException e
)
{
logger.
error(e,e
);
throw new ServiceException
(e.
toString(),e
);
}
}
else
{
throw new NotFoundException
("Directory does not exist with that ID " + directory.
getId());
}
}
}