-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-1797: Add JPA based SnapshotDao implementation
- Loading branch information
Yevhenii Voevodin
committed
Jul 25, 2016
1 parent
6ad68ae
commit 6b9cd11
Showing
27 changed files
with
615 additions
and
282 deletions.
There are no files selected for viewing
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
135 changes: 135 additions & 0 deletions
135
...core-api-machine/src/main/java/org/eclipse/che/api/machine/server/jpa/JpaSnapshotDao.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,135 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.api.machine.server.jpa; | ||
|
||
import com.google.inject.persist.Transactional; | ||
|
||
import org.eclipse.che.api.core.NotFoundException; | ||
import org.eclipse.che.api.core.ServerException; | ||
import org.eclipse.che.api.core.jdbc.jpa.DuplicateKeyException; | ||
import org.eclipse.che.api.machine.server.exception.SnapshotException; | ||
import org.eclipse.che.api.machine.server.model.impl.SnapshotImpl; | ||
import org.eclipse.che.api.machine.server.spi.SnapshotDao; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
import javax.inject.Singleton; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.NoResultException; | ||
import java.util.List; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* JPA based {@link SnapshotDao} implementation. | ||
* | ||
* @author Yevhenii Voevodin | ||
*/ | ||
@Singleton | ||
public class JpaSnapshotDao implements SnapshotDao { | ||
|
||
@Inject | ||
private Provider<EntityManager> managerProvider; | ||
|
||
@Override | ||
@Transactional | ||
public SnapshotImpl getSnapshot(String snapshotId) throws NotFoundException, SnapshotException { | ||
requireNonNull(snapshotId, "Required non-null snapshotId"); | ||
try { | ||
final SnapshotImpl snapshot = managerProvider.get().find(SnapshotImpl.class, snapshotId); | ||
if (snapshot == null) { | ||
throw new NotFoundException(format("Snapshot with id '%s' doesn't exist", snapshotId)); | ||
} | ||
return snapshot; | ||
} catch (RuntimeException x) { | ||
throw new SnapshotException(x.getLocalizedMessage(), x); | ||
} | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public SnapshotImpl getSnapshot(String workspaceId, String envName, String machineName) throws NotFoundException, SnapshotException { | ||
requireNonNull(workspaceId, "Required non-null workspace id"); | ||
requireNonNull(envName, "Required non-null environment name"); | ||
requireNonNull(machineName, "Required non-null machine name"); | ||
try { | ||
return managerProvider.get() | ||
.createNamedQuery("Snapshot.getByMachine", SnapshotImpl.class) | ||
.setParameter("workspaceId", workspaceId) | ||
.setParameter("envName", envName) | ||
.setParameter("machineName", machineName) | ||
.getSingleResult(); | ||
} catch (NoResultException x) { | ||
throw new NotFoundException(format("Snapshot for machine '%s:%s:%s' doesn't exist", | ||
workspaceId, | ||
envName, | ||
machineName)); | ||
} catch (RuntimeException x) { | ||
throw new SnapshotException(x.getLocalizedMessage(), x); | ||
} | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public List<SnapshotImpl> findSnapshots(String workspaceId) throws SnapshotException { | ||
requireNonNull(workspaceId, "Required non-null workspace id"); | ||
try { | ||
return managerProvider.get() | ||
.createNamedQuery("Snapshot.findSnapshots", SnapshotImpl.class) | ||
.setParameter("workspaceId", workspaceId) | ||
.getResultList(); | ||
} catch (RuntimeException x) { | ||
throw new SnapshotException(x.getLocalizedMessage(), x); | ||
} | ||
} | ||
|
||
@Override | ||
public void saveSnapshot(SnapshotImpl snapshot) throws SnapshotException { | ||
requireNonNull(snapshot, "Required non-null snapshot"); | ||
try { | ||
doSave(snapshot); | ||
} catch (DuplicateKeyException x) { | ||
throw new SnapshotException(format("Snapshot with id '%s' or for machine '%s:%s:%s' already exists", | ||
snapshot.getId(), | ||
snapshot.getWorkspaceId(), | ||
snapshot.getEnvName(), | ||
snapshot.getMachineName())); | ||
} catch (RuntimeException x) { | ||
throw new SnapshotException(x.getLocalizedMessage(), x); | ||
} | ||
} | ||
|
||
@Override | ||
public void removeSnapshot(String snapshotId) throws NotFoundException, SnapshotException { | ||
requireNonNull(snapshotId, "Required non-null snapshot id"); | ||
try { | ||
doRemove(snapshotId); | ||
} catch (RuntimeException x) { | ||
throw new SnapshotException(x.getLocalizedMessage(), x); | ||
} | ||
} | ||
|
||
@Transactional | ||
protected void doSave(SnapshotImpl snapshot) { | ||
managerProvider.get().persist(snapshot); | ||
} | ||
|
||
@Transactional | ||
protected void doRemove(String snapshotId) throws NotFoundException { | ||
final EntityManager manager = managerProvider.get(); | ||
final SnapshotImpl snapshot = manager.find(SnapshotImpl.class, snapshotId); | ||
if (snapshot == null) { | ||
throw new NotFoundException(format("Snapshot with id '%s' doesn't exist", snapshotId)); | ||
} | ||
manager.remove(snapshot); | ||
} | ||
} |
Oops, something went wrong.