Commit 526465e0 authored by Matija Obreza's avatar Matija Obreza
Browse files

Adding a Method creates the data table

parent 83f0a642
......@@ -16,6 +16,7 @@
package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
......@@ -30,4 +31,6 @@ public interface GenesysLowlevelRepository {
*/
void updateFaoInstituteCountries();
void ensureMethodTable(Method method);
}
......@@ -16,10 +16,15 @@
package org.genesys2.server.persistence.domain;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -46,7 +51,6 @@ public class GenesysLowlevelRepositoryImpl implements GenesysLowlevelRepository
int count = jdbcTemplate.update("UPDATE `accession` A inner join `faoinstitute` FI on FI.code=A.Institute set A.instituteId=FI.id;");
LOG.info("Institute references updated: " + count);
LOG.info("Updating institute accession count");
count = jdbcTemplate
.update("UPDATE faoinstitute FI inner join (select instituteId, count(*) X from accession group by instituteId) A on A.instituteId=FI.id set FI.accessionCount=A.X;");
......@@ -61,7 +65,6 @@ public class GenesysLowlevelRepositoryImpl implements GenesysLowlevelRepository
int count = jdbcTemplate.update("UPDATE `accession` A inner join `country` C on C.code3=A.Origin set A.originId=C.id WHERE A.Origin IS NOT NULL;");
LOG.info("Country references updated: " + count);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
......@@ -71,4 +74,60 @@ public class GenesysLowlevelRepositoryImpl implements GenesysLowlevelRepository
int count = jdbcTemplate.update("UPDATE `faoinstitute` I inner join `country` C on C.code3=left(I.code, 3) set I.countryId=C.id;");
LOG.info("Country references updated: " + count);
}
@Override
public void ensureMethodTable(Method method) {
LOG.info("Ensuring method table for " + method);
try {
LOG.info("Checking for table with name " + method.getId().toString());
DatabaseMetaData databaseMetadata = jdbcTemplate.getDataSource().getConnection().getMetaData();
ResultSet matchingTables = databaseMetadata.getTables(null, null, method.getId().toString(), new String[] { "TABLE" });
if (matchingTables.next()) {
LOG.info("Found existing table: " + matchingTables.getObject(3));
throw new RuntimeException("Method data table already exists!");
} else {
/*
* CREATE TABLE `100` ( `id` bigint(20) NOT NULL AUTO_INCREMENT,
* `ALIS_Id` bigint(20) NOT NULL, `Meta_id` int(6) NOT NULL,
* `LEAFSTREAK` char(2) NOT NULL, PRIMARY KEY (`id`), KEY
* `ALIS_Id` (`ALIS_Id`), KEY `LEAFSTREAK` (`LEAFSTREAK`) )
* ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
StringBuilder createTableSql = new StringBuilder();
// Common table elements
createTableSql.append("CREATE TABLE `").append(method.getId())
.append("` (`id` BIGINT NOT NULL AUTO_INCREMENT, `ALIS_Id` BIGINT NOT NULL, `Meta_id` INT NOT NULL, ");
// Value column
createTableSql.append("`").append(method.getFieldName()).append("` ");
createTableSql.append(getValueColumnDDL(method));
createTableSql.append(" NOT NULL");
// PK
createTableSql.append(", PRIMARY KEY (`id`), KEY `ALIS_Id` (`ALIS_Id`)");
// This is optional, but let's keep it
createTableSql.append(", KEY `").append(method.getFieldName()).append("` (`").append(method.getFieldName()).append("`)");
createTableSql.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
LOG.warn("Executing: " + createTableSql.toString());
jdbcTemplate.execute(createTableSql.toString());
}
LOG.info("Done");
} catch (SQLException e) {
LOG.error(e);
}
}
private String getValueColumnDDL(Method method) {
if (method.getFieldType() == 0) {
return "VARCHAR(" + method.getFieldSize() + ")";
} else if (method.getFieldType() == 1) {
return "DOUBLE";
} else if (method.getFieldType() == 2) {
return "BIGINT";
}
throw new RuntimeException("Unsupported data type " + method.getFieldType());
}
}
......@@ -21,6 +21,7 @@ import java.util.List;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
public interface MethodRepository extends JpaRepository<Method, Long> {
List<Method> findByFieldType(int fieldType);
......@@ -28,4 +29,7 @@ public interface MethodRepository extends JpaRepository<Method, Long> {
List<Method> findByParameter(Parameter parameter);
Method findByIdAndParameter(long id, Parameter findOne);
@Query("select distinct m from Method m where m.id in ( ?1 )")
List<Method> findByIds(List<Long> oids);
}
......@@ -14,7 +14,6 @@
* limitations under the License.
**/
package org.genesys2.server.service;
import java.util.List;
......@@ -25,6 +24,7 @@ import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.impl.Crop;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
public interface TraitService {
......@@ -42,4 +42,37 @@ public interface TraitService {
Page<Method> listMethods(Pageable pageable);
/**
* Add new crop descriptor
*
* @param crop
* @param title
* @return
*/
@PreAuthorize("isAuthenticated()")
Parameter addParameter(Crop crop, String title);
/**
* Add new method
*
* @param description
* @param unit
* @param fieldName
* @param fieldType
* @param options
* @param range
* @param parameter
* Optional, {@link Parameter}
* @return
*/
@PreAuthorize("isAuthenticated()")
Method addMethod(String description, String unit, String fieldName, int fieldType, String options, String range, Parameter parameter);
/**
* List user's methods
*
* @return
*/
List<Method> listMyMethods();
}
......@@ -409,4 +409,54 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
public Metadata getDataset(long id) {
return metadataRepository.findOne(id);
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Parameter addParameter(Crop crop, String title) {
Parameter parameter = new Parameter();
parameter.setCrop(crop);
parameter.setTitle(htmlSanitizer.sanitize(title));
parameterRepository.save(parameter);
return parameter;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Method addMethod(String description, String unit, String fieldName, int fieldType, String options, String range, Parameter parameter) {
Method method = new Method();
method.setMethod(htmlSanitizer.sanitize(description));
method.setUnit(unit);
method.setFieldName(fieldName);
method.setFieldType(fieldType);
method.setOptions(options);
method.setRange(range);
method.setParameter(parameter);
methodRepository.save(method);
// Ensure table for method!
genesysLowlevelRepository.ensureMethodTable(method);
return method;
}
/**
* Returns datasets to which current user has 'WRITE'
*/
@Override
@PreAuthorize("isAuthenticated()")
public List<Method> listMyMethods() {
AuthUserDetails user = SecurityContextUtil.getAuthUser();
List<Long> oids = aclService.listIdentitiesForSid(Method.class, user, BasePermission.WRITE);
LOG.info("Got " + oids.size() + " elements for " + user.getUsername());
if (oids.size() == 0)
return null;
return methodRepository.findByIds(oids);
}
}
Markdown is supported
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