package pt.estgp.estgweb.services.pageContent.dto;
import jomm.dao.impl.AbstractDao;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import pt.estgp.estgweb.domain.PageContent;
import pt.estgp.estgweb.domain.PageContentImpl;
import pt.estgp.estgweb.domain.PageSection;
import pt.estgp.estgweb.domain.PageSectionImpl;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jorgemachado on 11/01/17.
*/
public class VfsClient
{
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(VfsClient.
class);
public VfsAbstractPageContentDto selectPath
(1.5.0/docs/api/java/lang/String.html">String path
)
{
PageContent pageContent = DaoFactory.
getPageContentDaoImpl().
findByPath(path
);
if(pageContent ==
null)
{
return null;
}
else
{
return VfsAbstractPageContentDto.
initDto(pageContent
);
}
}
public VfsAbstractPageContentDto selectById
(1.5.0/docs/api/java/lang/Long.html">Long id
)
{
PageContent pageContent = DaoFactory.
getPageContentDaoImpl().
get(id
);
if(pageContent ==
null)
{
return null;
}
else
{
return VfsAbstractPageContentDto.
initDto(pageContent
);
}
}
/**
* Roots must have a string as slug at first place there are no roots like "" or "/"
* @param slug
*/
public VfsPageSectionDto createRootVfsPageSectionDto
(1.5.0/docs/api/java/lang/String.html">String slug
)
{
checkSlug
(slug
);
PageSection pageSection =
new PageSectionImpl
();
pageSection.
setSlug(slug
);
pageSection.
setPath("/" + slug
);
pageSection.
setTopLayoutSection(true);
pageSection.
setVisible(true);
pageSection.
setTitle("");
pageSection.
setDescription("");
try {
pageSection.
setAclList(PageContentImpl.
1.5.0/docs/api/java/security/acl/Acl.html">Acl.
AUTHENTICATED_ACL_SIMPLE_LIST);
} catch (1.5.0/docs/api/java/lang/Exception.html">Exception e
) {
logger.
error(e, e
);
}
DaoFactory.
getPageSectionDaoImpl().
save(pageSection
);
return new VfsPageSectionDto
(pageSection
);
}
public VfsPageSectionDto findOrCreateSectionVfsPathDto
(1.5.0/docs/api/java/lang/String.html">String path
)
{
if(path.
startsWith("/"))
path = path.
substring(1
);
if(path.
endsWith("/"))
path = path.
substring(0,path.
length()-1
);
1.5.0/docs/api/java/lang/String.html">String[] pathElements = path.
split("/");
for(1.5.0/docs/api/java/lang/String.html">String slug: pathElements
)
checkSlug
(slug
);
VfsPageSectionDto parent =
null;
1.5.0/docs/api/java/lang/String.html">String pathBuild =
"";
for(1.5.0/docs/api/java/lang/String.html">String slug: pathElements
)
{
VfsAbstractPageContentDto pContent = selectPath
(pathBuild +
"/" + slug
);
if(pContent
!=
null && !(pContent
instanceof VfsPageSectionDto
))
{
throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("Trying to create Section with path: " +
(pathBuild +
"/" + slug
) +
" where " + pContent.
getClass().
getName() +
" is not a content of type Section");
}
else if(pContent
!=
null)
{
parent =
(VfsPageSectionDto
) pContent
;
}
else
{
if(parent ==
null)
{
logger.
info("Creating new ROOT Section in VFS: " +
"/" + slug
);
parent = createRootVfsPageSectionDto
(slug
);
}
else
{
logger.
info("Creating new Section in VFS: " + pathBuild +
"/" + slug
);
parent = parent.
addVfsSection(slug,
"",
"",PageContentImpl.
1.5.0/docs/api/java/security/acl/Acl.html">Acl.
AUTHENTICATED_ACL_SIMPLE_LIST);
}
}
pathBuild = pathBuild +
"/" + slug
;
}
return parent
;
}
public void checkSlug
(1.5.0/docs/api/java/lang/String.html">String slug
)
{
if(slug ==
null || slug.
length() == 0 || slug.
contains("/"))
throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("Slug must be a simple string");
for(char c: slug.
toCharArray())
{
if(!(((c
>=
'a' && c
<=
'z') || c
>=
'A' && c
<=
'Z') || c ==
'-' || c ==
'_'))
throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException("Slug must be a simple string from a to z or A to Z or _ or -");
}
}
public static void main
(1.5.0/docs/api/java/lang/String.html">String[] args
)
{
AbstractDao.
getCurrentSession().
beginTransaction();
new VfsClient
().
findOrCreateSectionVfsPathDto("/publicitacaoIntitucional/deliberacoesPresidencia");
AbstractDao.
getCurrentSession().
getTransaction().
commit();
}
public List
<VfsPageSectionDto
> getRoots
()
{
List
<VfsPageSectionDto
> rootsDtos =
new ArrayList
<VfsPageSectionDto
>();
List
<PageSection
> roots = DaoFactory.
getPageSectionDaoImpl().
findRoots();
for(PageSection root: roots
)
{
rootsDtos.
add(new VfsPageSectionDto
(root
));
}
return rootsDtos
;
}
public 1.5.0/docs/api/java/lang/String.html">String getRootsJson
() throws JSONException
{
return getRootsJsonArray
().
toString();
}
public JSONArray getRootsJsonArray
() throws JSONException
{
JSONArray jsonArray =
new JSONArray
();
List
<VfsPageSectionDto
> rootsDtos = getRoots
();
for(VfsPageSectionDto dto: rootsDtos
)
{
jsonArray.
put(dto.
toJsonObject());
}
return jsonArray
;
}
}