From 1be66b2bd053435d7eaf21242c228d65c4768cab Mon Sep 17 00:00:00 2001 From: chenjing Date: Mon, 13 May 2019 17:44:08 -0700 Subject: [PATCH] Bug fix. Fixed regression in the update aspects function UD-287. Also fixed a bug that the update aspect function didn't update the modification time of the network. --- .../common/models/dao/postgresql/NetworkDAO.java | 3 ++- .../common/persistence/CXNetworkAspectsUpdater.java | 11 ++++++----- .../ndexbio/common/persistence/CXNetworkLoader.java | 11 ++++++----- .../org/ndexbio/rest/services/NetworkServiceV2.java | 3 ++- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/ndexbio/common/models/dao/postgresql/NetworkDAO.java b/src/main/java/org/ndexbio/common/models/dao/postgresql/NetworkDAO.java index 2b524578..8e046730 100644 --- a/src/main/java/org/ndexbio/common/models/dao/postgresql/NetworkDAO.java +++ b/src/main/java/org/ndexbio/common/models/dao/postgresql/NetworkDAO.java @@ -390,9 +390,10 @@ public void clearNetworkSummary(UUID networkId, long fileSize) throws SQLExcepti * @throws NdexException * @throws JsonProcessingException */ - public void saveNetworkEntry(NetworkSummary networkSummary, MetaDataCollection metadata) throws SQLException, NdexException, JsonProcessingException { + public void saveNetworkEntry(NetworkSummary networkSummary, MetaDataCollection metadata, boolean setModificationTime) throws SQLException, NdexException, JsonProcessingException { String sqlStr = "update network set name = ?, description = ?, version = ?, edgecount=?, nodecount=?, " + "properties = ? ::jsonb, cxmetadata = ? :: json, warnings = ?, subnetworkids = ?, " + + (setModificationTime? "modification_time = localtimestamp, " : "") + " is_validated =true where \"UUID\" = ? and is_deleted = false"; try (PreparedStatement pst = db.prepareStatement(sqlStr)) { pst.setString(1,networkSummary.getName()); diff --git a/src/main/java/org/ndexbio/common/persistence/CXNetworkAspectsUpdater.java b/src/main/java/org/ndexbio/common/persistence/CXNetworkAspectsUpdater.java index bac81c08..d195ba19 100644 --- a/src/main/java/org/ndexbio/common/persistence/CXNetworkAspectsUpdater.java +++ b/src/main/java/org/ndexbio/common/persistence/CXNetworkAspectsUpdater.java @@ -46,10 +46,11 @@ public CXNetworkAspectsUpdater(UUID networkUUID, /*String ownerUserName,*/ Netwo public void update() throws FileNotFoundException, IOException, DuplicateObjectException, ObjectNotFoundException, NdexException, SQLException { try ( InputStream inputStream = new FileInputStream(Configuration.getInstance().getNdexRoot() + "/data/" + aspectsCXNetworkID.toString() + "/network.cx") ) { - persistNetworkData(inputStream); + persistNetworkData(inputStream, true); UUID networkUUID = getNetworkId(); + @SuppressWarnings("resource") NetworkDAO dao = getDAO(); //handle the network properties NetworkSummary summary = dao.getNetworkSummaryById(networkUUID); @@ -65,21 +66,21 @@ public void update() throws FileNotFoundException, IOException, DuplicateObjectE if ( aspectTable.containsKey(NodesElement.ASPECT_NAME)) summary.setNodeCount((int) aspectTable.get(NodesElement.ASPECT_NAME).getElementCount()); - summary.setModificationTime(new Timestamp(Calendar.getInstance().getTimeInMillis())); + //summary.setModificationTime(new Timestamp(Calendar.getInstance().getTimeInMillis())); - if ( aspectTable.containsKey(NetworkAttributesElement.ASPECT_NAME)) { + if ( aspectTable.containsKey(NetworkAttributesElement.ASPECT_NAME)) { summary.setProperties(properties); summary.setName(this.networkName); summary.setDescription(this.description); summary.setVersion(this.version); summary.setWarnings(warnings); - } + } if ( aspectTable.containsKey(SubNetworkElement.ASPECT_NAME)) { summary.setSubnetworkIds(subNetworkIds); } try { // dao.saveNetworkEntry(summary, (this.provenanceHistory == null? null: provenanceHistory.getEntity()), metadata); - dao.saveNetworkEntry(summary, fullMetaData); + dao.saveNetworkEntry(summary, fullMetaData ,true); dao.setFlag(getNetworkId(), "has_layout", fullMetaData.getMetaDataElement(CartesianLayoutElement.ASPECT_NAME)!=null); dao.commit(); } catch (SQLException e) { diff --git a/src/main/java/org/ndexbio/common/persistence/CXNetworkLoader.java b/src/main/java/org/ndexbio/common/persistence/CXNetworkLoader.java index 05a22c54..fa13bed6 100644 --- a/src/main/java/org/ndexbio/common/persistence/CXNetworkLoader.java +++ b/src/main/java/org/ndexbio/common/persistence/CXNetworkLoader.java @@ -146,7 +146,6 @@ public class CXNetworkLoader implements AutoCloseable { private Set indexedFields; // protected String updatedBy; - public CXNetworkLoader(UUID networkUUID, boolean isUpdate, NetworkDAO networkDao, VisibilityType visibility, Set IndexedFields, int sampleGenerationThreshold) { super(); @@ -233,7 +232,7 @@ public void persistCXNetwork() throws IOException, DuplicateObjectException, Obj try ( InputStream inputStream = new FileInputStream(Configuration.getInstance().getNdexRoot() + "/data/" + networkId + "/network.cx") ) { - persistNetworkData(inputStream); + persistNetworkData(inputStream, false); // logger.info("aspects have been stored."); @@ -259,7 +258,7 @@ public void persistCXNetwork() throws IOException, DuplicateObjectException, Obj summary.setSubnetworkIds(subNetworkIds); try { // dao.saveNetworkEntry(summary, (this.provenanceHistory == null? null: provenanceHistory.getEntity()), metadata); - dao.saveNetworkEntry(summary, metadata); + dao.saveNetworkEntry(summary, metadata, false); dao.commit(); } catch (SQLException e) { @@ -435,12 +434,14 @@ public void importNetwork() throws IOException, DuplicateObjectException, Object /** * If it is called from a network import function ( db migrator), we don't remove the provenance entry from the metadata. + * @param isAspectUpdate set this flag if updating aspects in network. We are not check if node aspect is missing when + * this parameter is set to true. * @throws IOException * @throws DuplicateObjectException * @throws NdexException * @throws ObjectNotFoundException */ - protected void persistNetworkData(InputStream in/*, boolean isImport*/) + protected void persistNetworkData(InputStream in, boolean isAspectUpdate) throws IOException, DuplicateObjectException, NdexException, ObjectNotFoundException { CxElementReader2 cxreader = createCXReader(in); @@ -529,7 +530,7 @@ protected void persistNetworkData(InputStream in/*, boolean isImport*/) if(metadata !=null) { - if (metadata.getMetaDataElement(NodesElement.ASPECT_NAME) == null ) { + if ( !isAspectUpdate && metadata.getMetaDataElement(NodesElement.ASPECT_NAME) == null ) { throw new NdexException ("Nodes aspect is missing."); } diff --git a/src/main/java/org/ndexbio/rest/services/NetworkServiceV2.java b/src/main/java/org/ndexbio/rest/services/NetworkServiceV2.java index b82a713d..77087bc0 100644 --- a/src/main/java/org/ndexbio/rest/services/NetworkServiceV2.java +++ b/src/main/java/org/ndexbio/rest/services/NetworkServiceV2.java @@ -1832,7 +1832,8 @@ public void deleteNetwork(final @PathParam("networkid") String id) throws NdexEx throw new NetworkConcurrentModificationException (); } throw new NdexException("Can't delete a read-only network."); - } + } + //TODO: need to check if the network actually exists and give an 404 error for that case. throw new NdexException("Only network owner can delete a network."); } catch ( IOException e ) { throw new NdexException ("Error occurred when deleting network: " + e.getMessage(), e);