Skip to content

Commit 823eee3

Browse files
Damiano Fiorenzavins01-4science
authored andcommitted
Merged in task/main-glam/GLAM-1116 (pull request DSpace#4731)
Task/main glam/GLAM-1116 Approved-by: Vincenzo Mecca
2 parents f42bb45 + 4faf3d3 commit 823eee3

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

dspace-api/src/main/java/org/dspace/authority/script/migrateEntity/MigrateEntity.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,53 +102,50 @@ public void internalRun() throws Exception {
102102
collections.add(col);
103103
}
104104

105-
106105
for (Collection collection : collections) {
107-
int countItems = 0;
106+
collection = context.reloadEntity(collection);
108107
int processedItems = 0;
109-
int notProcessed = 0;
110108
String currentEt = collection.getEntityType();
111109
String currentForm = collectionService.getMetadataFirstValue(collection,
112110
MetadataSchemaEnum.CRIS.getName(), "submission", "definition", Item.ANY);
113111
System.out
114112
.println("Current EntityType:" + currentEt + " and current form " +
115113
currentForm + " of collection with name:" + collection.getName());
114+
if (changeForm) {
115+
collectionService.setMetadataSingleValue(context, collection, MetadataSchemaEnum.CRIS.getName(),
116+
"submission", "definition", null,this.newFormName);
117+
}
116118
if (changeType) {
117119
collectionService.setMetadataSingleValue(context, collection, "dspace", "entity", "type", null,
118120
this.newEntityType);
119-
Iterator<Item> itemIterator = itemService.findAllByCollection(context, collection);
121+
Iterator<Item> itemIterator = itemService.findAllByOwningCollection(context, collection);
120122
handler.logInfo("Script start");
121123
while (itemIterator.hasNext()) {
122124
Item item = itemIterator.next();
123-
countItems++;
124-
String entityType = itemService.getMetadataFirstValue(item, "dspace",
125-
"entity", "type", Item.ANY);
126-
processedItems++;
127125
itemService.setMetadataSingleValue(context, item, "dspace", "entity", "type", null,
128126
this.newEntityType);
127+
processedItems++;
128+
context.uncacheEntity(item);
129+
if (processedItems % 100 == 0) {
130+
context.commit();
131+
}
129132
}
130133
}
131-
if (changeForm) {
132-
collectionService.setMetadataSingleValue(context, collection, MetadataSchemaEnum.CRIS.getName(),
133-
"submission", "definition", null ,this.newFormName);
134-
}
135-
collection = context.reloadEntity(collection);
136-
currentEt = collection.getEntityType();
137-
handler.logInfo("Processed colection : " + collection.getName());
138-
handler.logInfo("Found " + countItems + " items");
139134
handler.logInfo("Processed " + processedItems + " items");
140-
handler.logInfo("NOT processed " + notProcessed + " items");
135+
context.uncacheEntity(collection);
136+
context.commit();
141137
}
142-
context.commit();
143138
handler.logInfo("Script end");
144139
} catch (SQLException e) {
145-
context.rollback();
140+
if (context != null && context.isValid()) {
141+
context.abort();
142+
}
146143
log.error(e.getMessage(), e);
147144
throw new RuntimeException(e.getMessage(), e);
148145
} finally {
149-
if (context != null) {
146+
if (context != null && context.isValid()) {
150147
context.restoreAuthSystemState();
151-
context.close();
148+
context.complete();
152149
}
153150
}
154151
}

dspace-api/src/main/java/org/dspace/content/ItemServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,11 @@ public Iterator<Item> findAllByCollection(Context context, Collection collection
551551
return itemDAO.findAllByCollection(context, collection, limit, offset);
552552
}
553553

554+
@Override
555+
public Iterator<Item> findAllByOwningCollection(Context context, Collection collection) throws SQLException {
556+
return itemDAO.findAllByOwningCollection(context, collection);
557+
}
558+
554559
@Override
555560
public Iterator<Item> findInArchiveOrWithdrawnDiscoverableModifiedSince(Context context, Date since)
556561
throws SQLException {

dspace-api/src/main/java/org/dspace/content/dao/ItemDAO.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ Iterator<Item> findArchivedByCollectionExcludingOwning(Context context, Collecti
152152
Iterator<Item> findAllByCollection(Context context, Collection collection, Integer limit, Integer offset)
153153
throws SQLException;
154154

155+
/**
156+
* Returns all items (without filtering for withdrawn or in archive) with the given owning collection
157+
* @param context context
158+
* @param collection the collection
159+
* @return the item iterator containing the items in the collection
160+
* @throws SQLException If the query goes wrong
161+
*/
162+
public Iterator<Item> findAllByOwningCollection(Context context, Collection collection) throws SQLException;
163+
155164
/**
156165
* Count number of items in a given collection
157166
*

dspace-api/src/main/java/org/dspace/content/dao/impl/ItemDAOImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,17 @@ public Iterator<Item> findAllByCollection(Context context, Collection collection
420420
return new UUIDIterator<Item>(context, uuids, Item.class, this);
421421
}
422422

423+
@Override
424+
public Iterator<Item> findAllByOwningCollection(Context context, Collection collection)
425+
throws SQLException {
426+
Query query = createQuery(context, "SELECT item.id FROM Item as item " +
427+
"WHERE owningCollection = :collection");
428+
query.setParameter("collection", collection);
429+
@SuppressWarnings("unchecked")
430+
List<UUID> uuids = query.getResultList();
431+
return new UUIDIterator<Item>(context, uuids, Item.class, this);
432+
}
433+
423434
@Override
424435
public int countItems(Context context, Collection collection, boolean includeArchived, boolean includeWithdrawn,
425436
boolean discoverable)

dspace-api/src/main/java/org/dspace/content/service/ItemService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ Iterator<Item> findInArchiveOrWithdrawnNonDiscoverableModifiedSince(Context cont
270270
*/
271271
Iterator<Item> findAllByCollection(Context context, Collection collection) throws SQLException;
272272

273+
/**
274+
* Returns all items (without filtering for withdrawn or in archive) with the given owning collection
275+
* @param context context
276+
* @param collection the collection
277+
* @return the item iterator containing the items in the collection
278+
* @throws SQLException If the query goes wrong
279+
*/
280+
Iterator<Item> findAllByOwningCollection(Context context, Collection collection) throws SQLException;
281+
273282
/**
274283
* See whether this Item is contained by a given Collection.
275284
*

0 commit comments

Comments
 (0)