From e4eae802be39024de46045a310351480ccfbdfcd Mon Sep 17 00:00:00 2001 From: kaloyane <kaloyane> Date: Fri, 14 Jan 2011 15:06:03 +0000 Subject: [PATCH] [LMS-1936] fixed : attachment contents were not correctly deleted from the DB SVN: 19435 --- .../server/dataaccess/db/AttachmentDAO.java | 21 +++-- .../dataaccess/db/AttachmentDAOTest.java | 94 +++++++++++++++++++ 2 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAOTest.java diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAO.java index 0a6253f7238..121eb92f39b 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAO.java @@ -34,7 +34,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE; /** * Implementation of {@link IAttachmentDAO} for data bases. * - * @author Franz-Josef Elmer + * @author Franz-Josef Elmer * @author Tomasz Pylak */ final class AttachmentDAO extends AbstractGenericEntityDAO<AttachmentPE> implements IAttachmentDAO @@ -186,16 +186,23 @@ final class AttachmentDAO extends AbstractGenericEntityDAO<AttachmentPE> impleme final HibernateTemplate hibernateTemplate = getHibernateTemplate(); - final String query = - String.format("delete from %s where " + getParentName(owner) - + " = ? and fileName = ?", TABLE_NAME); - final int deletedRows = hibernateTemplate.bulkUpdate(query, toArray(owner, fileName)); + int deletedRows = 0; + for (AttachmentPE att : owner.getAttachments()) + { + if (fileName.equals(att.getFileName())) + { + deletedRows++; + hibernateTemplate.delete(att); + } + } + hibernateTemplate.flush(); + if (operationLog.isInfoEnabled()) { operationLog.debug(String.format( - "%s attachment(s) deleted for %s '%s' and file name '%s'.", deletedRows, owner - .getHolderName(), owner, fileName)); + "%s attachment(s) deleted for %s '%s' and file name '%s'.", deletedRows, + owner.getHolderName(), owner, fileName)); } return deletedRows; diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAOTest.java new file mode 100644 index 00000000000..7d7b0df6b3c --- /dev/null +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AttachmentDAOTest.java @@ -0,0 +1,94 @@ +/* + * Copyright 2007 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.openbis.generic.server.dataaccess.db; + +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotNull; +import static org.testng.AssertJUnit.assertNull; + +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.openbis.generic.server.dataaccess.IAttachmentDAO; +import ch.systemsx.cisd.openbis.generic.shared.dto.AttachmentContentPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.AttachmentPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE; + +/** + * Test cases for corresponding {@link AttachmentDAO} class. + * + * @author Kaloyan Enimanev + */ +@Test(groups = + { "db" }) +public final class AttachmentDAOTest extends AbstractDAOTest +{ + + private static final String FILE_NAME = "attachment-test.file"; + + private static final String ATT_CONTENTS_TABLE = "attachment_contents"; + + @Test + @Transactional(propagation = Propagation.NOT_SUPPORTED) + public final void testDeleteAttachment() + { + final int NUM_ATTACHMENTS = 2; + + IAttachmentDAO attachmentDAO = daoFactory.getAttachmentDAO(); + AttachmentPE attachment1 = createTestAttachment(1); + AttachmentPE attachment2 = createTestAttachment(2); + ExperimentPE owner = selectFirstExperiment(); + + int rowsInAttachmentContents = countRowsInTable(ATT_CONTENTS_TABLE); + + // create + attachmentDAO.createAttachment(attachment1, owner); + attachmentDAO.createAttachment(attachment2, owner); + + AttachmentPE persisted = + attachmentDAO.tryFindAttachmentByOwnerAndFileName(owner, FILE_NAME); + assertNotNull(persisted); + assertEquals(rowsInAttachmentContents + NUM_ATTACHMENTS, + countRowsInTable(ATT_CONTENTS_TABLE)); + + // delete and test + attachmentDAO.deleteByOwnerAndFileName(owner, FILE_NAME); + + persisted = attachmentDAO.tryFindAttachmentByOwnerAndFileName(owner, FILE_NAME); + assertNull(persisted); + assertEquals(rowsInAttachmentContents, countRowsInTable(ATT_CONTENTS_TABLE)); + + } + + private AttachmentPE createTestAttachment(int version) + { + AttachmentPE result = new AttachmentPE(); + result.setFileName(FILE_NAME); + result.setVersion(version); + + AttachmentContentPE content = new AttachmentContentPE(); + content.setValue("sample-attachment-content".getBytes()); + result.setAttachmentContent(content); + + result.setRegistrator(getTestPerson()); + + return result; + + } + +} \ No newline at end of file -- GitLab