Commit 1dc9b49b authored by Piotr Gawron's avatar Piotr Gawron
Browse files

directory is different with every add project request (it doesn't depend only on the project_id)

parent 253f652a
package lcsb.mapviewer.persist.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.dialect.Dialect;
import org.hibernate.jdbc.ReturningWork;
import org.hibernate.service.jdbc.dialect.internal.StandardDialectResolver;
import org.hibernate.service.jdbc.dialect.spi.DialectResolver;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.model.ModelData;
......@@ -12,56 +21,86 @@ import lcsb.mapviewer.model.map.model.ModelData;
*
*/
public class ProjectDao extends BaseDao<Project> {
/**
* Default constructor.
*/
public ProjectDao() {
super(Project.class);
}
/**
* Default constructor.
*/
public ProjectDao() {
super(Project.class);
}
/**
* Returns project with the given {@link Project#projectId project identifier} .
*
* @param projectId
* {@link Project#projectId project identifier}
* @return project for the given name
*/
public Project getProjectByProjectId(String projectId) {
Project project = getByParameter("projectId", projectId);
return project;
}
/**
* Returns project with the given {@link Project#projectId project identifier}
* .
*
* @param projectId
* {@link Project#projectId project identifier}
* @return project for the given name
*/
public Project getProjectByProjectId(String projectId) {
Project project = getByParameter("projectId", projectId);
/**
* Returns project that contains model with a given id.
*
* @param id
* id of the model
* @return project that contains model with a given id
*/
public Project getProjectForModelId(Integer id) {
List<?> list = getSession()
.createQuery("select project from " + ModelData.class.getSimpleName() + " model_t where model_t.id = :id")
.setParameter("id", id).list();
if (list.size() == 0) {
return null;
}
return (Project) list.get(0);
}
return project;
}
/**
* Returns information if the project with given name exists.
*
* @param projectName
* name of the project
* @return <code>true</code> if project with given name exists,
* <code>false</code> otherwise
*/
public boolean isProjectExistsByName(String projectName) {
@SuppressWarnings("unchecked")
List<Long> list = getSession()
.createQuery("select count(*) from " + getClazz().getSimpleName() + " where projectId = :projectId")
.setParameter("projectId", projectName).list();
return list.get(0) > 0;
}
/**
* Returns project that contains model with a given id.
*
* @param id
* id of the model
* @return project that contains model with a given id
*/
public Project getProjectForModelId(Integer id) {
List<?> list = getSession()
.createQuery("select project from " + ModelData.class.getSimpleName() + " model_t where model_t.id = :id").setParameter("id", id).list();
if (list.size() == 0) {
return null;
}
return (Project) list.get(0);
}
public long getNextId() {
ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
@Override
public Long execute(Connection connection) throws SQLException {
DialectResolver dialectResolver = new StandardDialectResolver();
Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString("project_table_iddb_seq"));
resultSet = preparedStatement.executeQuery();
resultSet.next();
return resultSet.getLong(1);
} catch (SQLException e) {
throw e;
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (resultSet != null) {
resultSet.close();
}
}
/**
* Returns information if the project with given name exists.
*
* @param projectName
* name of the project
* @return <code>true</code> if project with given name exists,
* <code>false</code> otherwise
*/
public boolean isProjectExistsByName(String projectName) {
@SuppressWarnings("unchecked")
List<Long> list = getSession()
.createQuery("select count(*) from " + getClazz().getSimpleName() + " where projectId = :projectId").setParameter("projectId", projectName).list();
return list.get(0) > 0;
}
}
};
return getSession().doReturningWork(maxReturningWork);
}
}
......@@ -66,6 +66,7 @@ import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.user.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
import lcsb.mapviewer.services.SecurityException;
import lcsb.mapviewer.services.interfaces.ILayoutService;
......@@ -103,6 +104,9 @@ public class ProjectRestImpl extends BaseRestImpl {
@Autowired
private IProjectService projectService;
@Autowired
private ProjectDao projectDao;
@Autowired
private OverviewImageViewFactory factory;
......@@ -493,10 +497,11 @@ public class ProjectRestImpl extends BaseRestImpl {
User user = getUserService().getUserByToken(authenticationToken);
Project project = getProjectService().getProjectByProjectId(projectId, authenticationToken);
if (project != null) {
logger.debug(project.getProjectId());
throw new ObjectExistsException("Project with given id already exists");
}
CreateProjectParams params = new CreateProjectParams();
String directory = path + "/../map_images/" + md5(projectId) + "/";
String directory = computePathForProject(projectId, path);
String fileId = getFirstValue(data.get("file-id"));
if (fileId == null) {
throw new QueryException("file-id is obligatory");
......@@ -552,6 +557,12 @@ public class ProjectRestImpl extends BaseRestImpl {
return getProject(projectId, token);
}
protected String computePathForProject(String projectId, String path) {
long id = projectDao.getNextId();
logger.debug(id);
return path + "/../map_images/" + md5(projectId + "-" + id) + "/";
}
protected List<ZipEntryFile> extractZipEntries(MultiValueMap<String, Object> data) {
int fileIndex = 0;
List<ZipEntryFile> result = new ArrayList<>();
......
package lcsb.mapviewer.api.projects;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
......@@ -40,6 +41,7 @@ import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.cache.FileEntry;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.IProjectService;
......@@ -51,6 +53,11 @@ public class ProjectRestImplTest extends RestTestFunctions {
@Autowired
IModelService modelService;
@Autowired
IProjectService projectService;
@Autowired
ProjectDao projectDao;
@AfterClass
public static void tearDownAfterClass() throws Exception {
......@@ -194,6 +201,25 @@ public class ProjectRestImplTest extends RestTestFunctions {
}
}
@Test
public void testComputePathForProject() throws Exception {
try {
String project_id = "Some_id";
String directory1 = _projectRestImpl.computePathForProject(project_id, ".");
Project project = new Project();
project.setProjectId(project_id);
projectDao.add(project);
projectDao.delete(project);
String directory2 = _projectRestImpl.computePathForProject(project_id, ".");
assertFalse(directory1.equals(directory2));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testGetProjects() throws Exception {
try {
......@@ -246,25 +272,25 @@ public class ProjectRestImplTest extends RestTestFunctions {
public void testExtractZipEntries() throws Exception {
try {
MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();
data.put("zip-entries[0][_type]",createLinkedList("MAP"));
data.put("zip-entries[0][_filename]",createLinkedList("main.xml"));
data.put("zip-entries[0][_data][root]",createLinkedList("true"));
data.put("zip-entries[0][_data][name]",createLinkedList("main"));
data.put("zip-entries[0][_data][type][id]",createLinkedList("UNKNOWN"));
data.put("zip-entries[0][_data][type][name]",createLinkedList("Unknown"));
data.put("zip-entries[1][_type]",createLinkedList("OVERLAY"));
data.put("zip-entries[1][_filename]",createLinkedList("layouts/goodschema.txt"));
data.put("zip-entries[1][_data][name]",createLinkedList("example name"));
data.put("zip-entries[1][_data][description]",createLinkedList("layout description"));
data.put("zip-entries[2][_type]",createLinkedList("IMAGE"));
data.put("zip-entries[2][_filename]",createLinkedList("images/test.png"));
data.put("zip-entries[0][_type]", createLinkedList("MAP"));
data.put("zip-entries[0][_filename]", createLinkedList("main.xml"));
data.put("zip-entries[0][_data][root]", createLinkedList("true"));
data.put("zip-entries[0][_data][name]", createLinkedList("main"));
data.put("zip-entries[0][_data][type][id]", createLinkedList("UNKNOWN"));
data.put("zip-entries[0][_data][type][name]", createLinkedList("Unknown"));
data.put("zip-entries[1][_type]", createLinkedList("OVERLAY"));
data.put("zip-entries[1][_filename]", createLinkedList("layouts/goodschema.txt"));
data.put("zip-entries[1][_data][name]", createLinkedList("example name"));
data.put("zip-entries[1][_data][description]", createLinkedList("layout description"));
data.put("zip-entries[2][_type]", createLinkedList("IMAGE"));
data.put("zip-entries[2][_filename]", createLinkedList("images/test.png"));
List<ZipEntryFile> result = _projectRestImpl.extractZipEntries(data);
assertNotNull(result);
assertEquals(3, result.size());
assertTrue(result.get(0) instanceof ModelZipEntryFile);
assertTrue(result.get(1) instanceof LayoutZipEntryFile);
assertTrue(result.get(2) instanceof ImageZipEntryFile);
} catch (Exception e) {
e.printStackTrace();
throw e;
......@@ -305,7 +331,7 @@ public class ProjectRestImplTest extends RestTestFunctions {
public void testGetLogs() throws Exception {
try {
ProjectRestImpl projectRest = createMockProjectRest("testFiles/model/sample.xml");
Map<String,Object> result = projectRest.getLogs("sample", "", token.getId(), "0", 10, "id", "asc", "");
Map<String, Object> result = projectRest.getLogs("sample", "", token.getId(), "0", 10, "id", "asc", "");
assertNotNull(result);
Gson gson = new Gson();
assertNotNull(gson.toJson(result));
......@@ -315,5 +341,4 @@ public class ProjectRestImplTest extends RestTestFunctions {
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment