-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
2,693 additions
and
191 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...src/main/java/org/eclipse/vorto/repository/internal/service/DefaultRepositoryManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package org.eclipse.vorto.repository.internal.service; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.InputStream; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import javax.jcr.ImportUUIDBehavior; | ||
import javax.jcr.Item; | ||
import javax.jcr.PathNotFoundException; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
|
||
import org.eclipse.vorto.repository.model.ModelId; | ||
import org.eclipse.vorto.repository.model.ModelResource; | ||
import org.eclipse.vorto.repository.service.FatalModelRepositoryException; | ||
import org.eclipse.vorto.repository.service.IModelRepository; | ||
import org.eclipse.vorto.repository.service.IRepositoryManager; | ||
import org.eclipse.vorto.repository.service.ModelReferentialIntegrityException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class DefaultRepositoryManager implements IRepositoryManager { | ||
|
||
@Autowired | ||
private Session session; | ||
|
||
@Autowired | ||
private IModelRepository modelRepository; | ||
|
||
@Override | ||
public byte[] backup() throws Exception { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
((org.modeshape.jcr.api.Session) session).exportDocumentView("/", baos, false, false); | ||
baos.close(); | ||
return baos.toByteArray(); | ||
|
||
} | ||
|
||
@Override | ||
public void restore(InputStream inputStream) throws Exception { | ||
removeAll(); | ||
((org.modeshape.jcr.api.Session) session).getWorkspace().importXML("/", inputStream, | ||
ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW); | ||
} | ||
|
||
public Session getSession() { | ||
return session; | ||
} | ||
|
||
public void setSession(Session session) { | ||
this.session = session; | ||
} | ||
|
||
public IModelRepository getModelRepository() { | ||
return modelRepository; | ||
} | ||
|
||
public void setModelRepository(IModelRepository modelRepository) { | ||
this.modelRepository = modelRepository; | ||
} | ||
|
||
@Override | ||
public void removeModel(ModelId modelId) { | ||
try { | ||
ModelResource modelResource = this.modelRepository.getById(modelId); | ||
if (!modelResource.getReferencedBy().isEmpty()) { | ||
throw new ModelReferentialIntegrityException( | ||
"Cannot remove model because it is referenced by other model(s)", | ||
modelResource.getReferencedBy()); | ||
} | ||
Item item = session.getItem(modelId.getFullPath()); | ||
item.remove(); | ||
session.save(); | ||
} catch (RepositoryException e) { | ||
throw new FatalModelRepositoryException("Problem occured removing the model", e); | ||
} | ||
} | ||
|
||
private void removeAll() throws Exception { | ||
Set<String> rootNodes = new HashSet<>(); | ||
for (ModelResource resource : this.modelRepository.search("*")) { | ||
final String org = resource.getId().getNamespace().substring(0, | ||
resource.getId().getNamespace().indexOf(".")); | ||
rootNodes.add(org); | ||
} | ||
|
||
for (String rootNode : rootNodes) { | ||
try { | ||
Item item = session.getItem("/" + rootNode); | ||
item.remove(); | ||
} catch (PathNotFoundException ex) { | ||
} | ||
} | ||
|
||
this.session.save(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...repo/repo-core/src/main/java/org/eclipse/vorto/repository/service/IRepositoryManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.eclipse.vorto.repository.service; | ||
|
||
import java.io.InputStream; | ||
|
||
import org.eclipse.vorto.repository.model.ModelId; | ||
|
||
public interface IRepositoryManager { | ||
|
||
byte[] backup() throws Exception; | ||
|
||
void restore(InputStream inputStream) throws Exception; | ||
|
||
void removeModel(ModelId modelId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.