Skip to content

Commit

Permalink
internal/migrate-clarin-dspace5-to-clarin-dspace7-continue (#220)
Browse files Browse the repository at this point in the history
* export table data as json

* controller for group import

* upgrade values type in database

* alter table

* controller for group2group import

* controller for group2eperson

* Removed varchar limitations (#218)

Co-authored-by: MilanMajchrák <milan.majchak@dataquest.sk>

* create new cntroller for item and service for workspaceitem

* changed Controller from ItemRest to WorkspaceRest

* added test

* fix error in controller

* create workflowitem from workspaceitem

* problem with adding submitter and creating workflowitem

* import workflowitem and item

* fix incorrect class in log'

* 'reate bitstream without file, error with protected consturctor

* test for create bundle for existing file

* missing committed xml file

* fix errors for bitstream validation

* correction of the test errors

* added primary bitstream to bitstream

* test for import items

* fix item tests - inArchive

* fix import item tests

* correction of found errors

* controller for import eperson and user_registration

* import logo of collection and community

* modified bitstream import and added most_recent_checksum import

* fix specifically date format

* added test for import logo

* new builder for bitstream without item and bundle, fix test errors

* checkstyle and comments

* fix tests

* checkstyle and fix test

* fix failed test

* Fixed review issues.

* 74-BE license definition missing attribute values

* Fixed review requests.

* Removed unnecessary files

---------

Co-authored-by: milanmajchrak <90026355+milanmajchrak@users.noreply.github.com>
Co-authored-by: MilanMajchrák <milan.majchak@dataquest.sk>
  • Loading branch information
3 people committed Jun 19, 2024
1 parent 6b58fa8 commit 63c9de3
Show file tree
Hide file tree
Showing 28 changed files with 2,576 additions and 36 deletions.
3 changes: 3 additions & 0 deletions dspace-api/src/main/java/org/dspace/content/Bitstream.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.clarin.ClarinBitstreamService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.hibernate.proxy.HibernateProxyHelper;
Expand Down Expand Up @@ -83,6 +84,8 @@ public class Bitstream extends DSpaceObject implements DSpaceObjectLegacySupport
* {@link org.dspace.content.service.BitstreamService#create(Context, Bundle, InputStream)}
* or
* {@link org.dspace.content.service.BitstreamService#create(Context, InputStream)}
* or
* {@link ClarinBitstreamService#create(Context, Bundle)}
*/
protected Bitstream() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.authorize.service.AuthorizeService;
import org.dspace.content.dao.BitstreamDAO;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.clarin.ClarinBitstreamService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.event.Event;
import org.dspace.storage.bitstore.DSBitStoreService;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Service interface class for the Bitstream object created for Clarin-Dspace import.
* Contains methods needed to import bitstream when dspace5 migrating to dspace7.
* The implementation of this class is autowired by spring.
* This class should never be accessed directly.
*
* @author Michaela Paurikova (michaela.paurikova at dataquest.sk)
*/
//If this class wants to catch the Bitstream protected constructor, it must be in this package!
public class ClarinBitstreamServiceImpl implements ClarinBitstreamService {
/**
* log4j logger
*/
private static Logger log = org.apache.logging.log4j.LogManager.getLogger(ClarinBitstreamServiceImpl.class);

// Checksum algorithm
private static final String CSA = "MD5";

@Autowired
private DSBitStoreService storeService;
@Autowired
protected BitstreamDAO bitstreamDAO;
@Autowired
protected AuthorizeService authorizeService;
@Autowired
protected BundleService bundleService;
@Autowired
protected BitstreamService bitstreamService;

protected ClarinBitstreamServiceImpl() {
}

@Override
public Bitstream create(Context context, Bundle bundle) throws SQLException, AuthorizeException {
if (!authorizeService.isAdmin(context)) {
throw new AuthorizeException(
"You must be an admin to create an empty bitstream");
}
//create empty bitstream
Bitstream bitstream = bitstreamDAO.create(context, new Bitstream());

// Set the format to "unknown"
bitstreamService.setFormat(context, bitstream, null);
context.addEvent(
new Event(Event.CREATE, Constants.BITSTREAM, bitstream.getID(),
null, bitstreamService.getIdentifiers(context, bitstream)));

//add bitstream to bundle if the bundle is entered
if (Objects.nonNull(bundle)) {
bundleService.addBitstream(context, bundle, bitstream);
}
log.debug("Created new empty Bitstream with id: " + bitstream.getID());
return bitstream;
}

@Override
public boolean addExistingFile(Context context, Bitstream bitstream, Long expectedSizeBytes,
String expectedCheckSum, String expectedChecksumAlgorithm)
throws IOException, SQLException, AuthorizeException {
if (!authorizeService.isAdmin(context)) {
throw new AuthorizeException(
"You must be an admin to add existing file to bitstream");
}
if (Objects.isNull(bitstream) || StringUtils.isBlank(bitstream.getInternalId())) {
throw new IllegalStateException(
"Cannot add file to bitstream because it is entered incorrectly.");
}
//get file from assetstore based on internal_id
//recalculate check fields
storeService.put(bitstream, new ByteArrayInputStream(storeService.get(bitstream).readAllBytes()));
//check that new calculated values match the expected values
if (!valid(bitstream, expectedSizeBytes, expectedCheckSum, expectedChecksumAlgorithm)) {
//an error occurred - expected and calculated values do not match
//delete all created data
bitstreamService.delete(context, bitstream);
bitstreamService.expunge(context, bitstream);
log.debug("Cannot add file with internal id: " +
bitstream.getInternalId() + " to bitstream with id: " + bitstream.getID()
+ " because the validation is incorrectly.");
return false;
}
bitstreamService.update(context, bitstream);
return true;
}

/**
* Validation control.
* Control that expected values (method attributes) match with bitstream calculated values.
* @param bitstream bitstream
* @param expectedSizeBytes expected size bytes
* @param expectedCheckSum expected checksum
* @param expectedChecksumAlgorithm expected checksum algorithm
* @return bitstream values match with expected values
*/
private boolean valid(Bitstream bitstream, long expectedSizeBytes,
String expectedCheckSum, String expectedChecksumAlgorithm) {
return bitstream.getSizeBytes() == expectedSizeBytes && bitstream.getChecksum().equals(expectedCheckSum) &&
bitstream.getChecksumAlgorithm().equals(expectedChecksumAlgorithm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,23 +365,31 @@ public Bitstream setLogo(Context context, Collection collection, InputStream is)
"collection_id=" + collection.getID()));
} else {
Bitstream newLogo = bitstreamService.create(context, is);
collection.setLogo(newLogo);

// now create policy for logo bitstream
// to match our READ policy
List<ResourcePolicy> policies = authorizeService
.getPoliciesActionFilter(context, collection, Constants.READ);
authorizeService.addPolicies(context, policies, newLogo);

log.info(LogHelper.getHeader(context, "set_logo",
"collection_id=" + collection.getID() + "logo_bitstream_id="
+ newLogo.getID()));
//added for data migration by Upgrade Dspace-Clarin
addLogo(context, collection, newLogo);
}

collection.setModified();
return collection.getLogo();
}

@Override
public void addLogo(Context context, Collection collection, Bitstream newLogo)
throws SQLException, AuthorizeException {
collection.setLogo(newLogo);

// now create policy for logo bitstream
// to match our READ policy
List<ResourcePolicy> policies = authorizeService
.getPoliciesActionFilter(context, collection, Constants.READ);
authorizeService.addPolicies(context, policies, newLogo);

log.info(LogHelper.getHeader(context, "set_logo",
"collection_id=" + collection.getID() + "logo_bitstream_id="
+ newLogo.getID()));
}

@Override
public Group createWorkflowGroup(Context context, Collection collection, int step)
throws SQLException, AuthorizeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public Community create(Community parent, Context context, String handle,
// of 'anonymous' READ
Group anonymousGroup = groupService.findByName(context, Group.ANONYMOUS);

authorizeService.createResourcePolicy(context, newCommunity, anonymousGroup, null, Constants.READ, null);
authorizeService.createResourcePolicy(context, newCommunity, anonymousGroup,null,
Constants.READ, null);

communityDAO.save(context, newCommunity);

Expand Down Expand Up @@ -241,22 +242,30 @@ public Bitstream setLogo(Context context, Community community, InputStream is)

if (is != null) {
Bitstream newLogo = bitstreamService.create(context, is);
community.setLogo(newLogo);

// now create policy for logo bitstream
// to match our READ policy
List<ResourcePolicy> policies = authorizeService
.getPoliciesActionFilter(context, community, Constants.READ);
authorizeService.addPolicies(context, policies, newLogo);

log.info(LogHelper.getHeader(context, "set_logo",
"community_id=" + community.getID() + "logo_bitstream_id="
+ newLogo.getID()));
//added for data migration by Upgrade Dspace-Clarin
addLogo(context, community, newLogo);
}

return community.getLogo();
}

@Override
public void addLogo(Context context, Community community, Bitstream newLogo)
throws SQLException, AuthorizeException {
community.setLogo(newLogo);

// now create policy for logo bitstream
// to match our READ policy
List<ResourcePolicy> policies = authorizeService
.getPoliciesActionFilter(context, community, Constants.READ);
authorizeService.addPolicies(context, policies, newLogo);

log.info(LogHelper.getHeader(context, "set_logo",
"community_id=" + community.getID() + "logo_bitstream_id="
+ newLogo.getID()));
}

@Override
public void update(Context context, Community community) throws SQLException, AuthorizeException {
// Check authorisation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.content.clarin;

import java.sql.SQLException;
import java.util.Objects;
import java.util.UUID;

import org.apache.logging.log4j.Logger;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Collection;
import org.dspace.content.WorkspaceItem;
import org.dspace.content.dao.WorkspaceItemDAO;
import org.dspace.content.service.WorkspaceItemService;
import org.dspace.content.service.clarin.ClarinWorkspaceItemService;
import org.dspace.core.Context;
import org.dspace.core.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Service interface class for the WorkspaceItem object created for Clarin-Dspace import.
* Contains methods needed to import bitstream when dspace5 migrating to dspace7.
* The implementation of this class is autowired by spring.
* This class should never be accessed directly.
*
* @author Michaela Paurikova(michaela.paurikova at dataquest.sk)
*/
public class ClarinWorkspaceItemServiceImpl implements ClarinWorkspaceItemService {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(
ClarinWorkspaceItemServiceImpl.class);
@Autowired
private WorkspaceItemService workspaceItemService;
@Autowired
private WorkspaceItemDAO workspaceItemDAO;

@Override
public WorkspaceItem create(Context context, Collection collection, boolean multipleTitles, boolean publishedBefore,
boolean multipleFiles, Integer stageReached, Integer pageReached,
boolean template) throws AuthorizeException, SQLException {

//create empty workspace item with item
WorkspaceItem workspaceItem = workspaceItemService.create(context, collection, false);
//set workspace item values based on input values
workspaceItem.setPublishedBefore(publishedBefore);
workspaceItem.setMultipleFiles(multipleFiles);
workspaceItem.setMultipleTitles(multipleTitles);
workspaceItem.setPageReached(pageReached);
workspaceItem.setStageReached(stageReached);
return workspaceItem;
}

@Override
public WorkspaceItem find(Context context, UUID uuid) throws SQLException {
//find workspace item by its UUID
WorkspaceItem workspaceItem = workspaceItemDAO.findByID(context, WorkspaceItem.class, uuid);

//create log if the workspace item is not found
if (log.isDebugEnabled()) {
if (Objects.nonNull(workspaceItem)) {
log.debug(LogHelper.getHeader(context, "find_workspace_item",
"not_found,workspace_item_uuid=" + uuid));
} else {
log.debug(LogHelper.getHeader(context, "find_workspace_item",
"workspace_item_uuid=" + uuid));
}
}
return workspaceItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ public Collection create(Context context, Community community, String handle, UU
public Bitstream setLogo(Context context, Collection collection, InputStream is) throws AuthorizeException,
IOException, SQLException;

/**
* Add the created logo bitstream to collection and create policy to logo bitstream.
* This method is added for data migration by Upgrade Clarin, where bitstream already exists.
* @param context context
* @param collection collection
* @param newLogo bitstream of new logo
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public void addLogo(Context context, Collection collection, Bitstream newLogo)
throws SQLException, AuthorizeException;
/**
* Create a workflow group for the given step if one does not already exist.
* Returns either the newly created group or the previously existing one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public Community create(Community parent, Context context,
public Bitstream setLogo(Context context, Community community, InputStream is) throws AuthorizeException,
IOException, SQLException;

/**
* Add the created logo bitstream to community and create policy to logo bitstream.
* This method is added for data migration by Upgrade Clarin, where bitstream already exists.
* @param context context
* @param community community
* @param newLogo bitstream of new logo
* @throws SQLException if database error
* @throws AuthorizeException if authorization error
*/
public void addLogo(Context context, Community community, Bitstream newLogo)
throws SQLException, AuthorizeException;
/**
* Create a default administrators group if one does not already exist.
* Returns either the newly created group or the previously existing one.
Expand Down
Loading

0 comments on commit 63c9de3

Please sign in to comment.