Java Code Examples for javax.jdo.PersistenceManager#deletePersistent()

The following examples show how to use javax.jdo.PersistenceManager#deletePersistent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
private void dropSentryRoleCore(PersistenceManager pm, String roleName)
    throws SentryNoSuchObjectException {
  String lRoleName = roleName.trim().toLowerCase();
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole sentryRole = (MSentryRole) query.execute(lRoleName);
  if (sentryRole == null) {
    throw new SentryNoSuchObjectException("Role: " + lRoleName + " doesn't exist");
  } else {
    pm.retrieve(sentryRole);
    int numPrivs = sentryRole.getPrivileges().size();
    sentryRole.removePrivileges();
    // with SENTRY-398 generic model
    sentryRole.removeGMPrivileges();
    privCleaner.incPrivRemoval(numPrivs);
    pm.deletePersistent(sentryRole);
  }
}
 
Example 2
Source File: GuideToJDO.java    From tutorials with MIT License 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public void DeleteProducts() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query query = pm.newQuery(Product.class, "name == \"Android Phone\"");
        Collection result = (Collection) query.execute();
        Product product = (Product) result.iterator().next();
        pm.deletePersistent(product);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
 
Example 3
Source File: GuideToJDO.java    From tutorials with MIT License 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public void listXMLProducts() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumdXML, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();

        Query q = pm.newQuery("SELECT FROM " + ProductXML.class.getName());
        List<ProductXML> products = (List<ProductXML>) q.execute();
        Iterator<ProductXML> iter = products.iterator();
        while (iter.hasNext()) {
            ProductXML p = iter.next();
            LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.getName(), p.getPrice() });
            pm.deletePersistent(p);
        }
        LOGGER.log(Level.INFO, "--------------------------------------------------------------");
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }

        pm.close();
    }
}
 
Example 4
Source File: UserStore.java    From two-token-sw with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the user for the email.
 * 
 * @param email the email address to be deleted
 * @return response code for the operation
 */
public static AccountOperationResponseCode delete(String email) {
  PersistenceManager pm = pmf.getPersistenceManager();
  try {
    UserRecord user = findUserByEmail(pm, email);
    if (user != null) {
      pm.deletePersistent(user);
      return AccountOperationResponseCode.OK;
    } else {
      return AccountOperationResponseCode.USER_NOT_FOUND;
    }
  } finally {
    pm.close();
  }
}
 
Example 5
Source File: UserStore.java    From two-token-sw with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the user for the email.
 * 
 * @param email the email address to be deleted
 * @return response code for the operation
 */
public static AccountOperationResponseCode delete(String email) {
  PersistenceManager pm = pmf.getPersistenceManager();
  try {
    UserRecord user = findUserByEmail(pm, email);
    if (user != null) {
      pm.deletePersistent(user);
      return AccountOperationResponseCode.OK;
    } else {
      return AccountOperationResponseCode.USER_NOT_FOUND;
    }
  } finally {
    pm.close();
  }
}
 
Example 6
Source File: AbstractFlowUserServlet.java    From google-oauth-java-client with Apache License 2.0 5 votes vote down vote up
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager();

  String userId = getUserId();

  ThreeLeggedFlow oauthFlow = newFlow(userId);
  oauthFlow.setJsonFactory(getJsonFactory());
  oauthFlow.setHttpTransport(getHttpTransport());

  try {
    Credential cred = oauthFlow.loadCredential(pm);

    if (cred != null && cred.isInvalid()) {
      pm.deletePersistent(cred);
      cred = null;
    }

    if (cred != null) {
      req.setAttribute(AUTH_CREDENTIAL, cred);
      try {
        // Invoke the user code
        super.service(req, resp);
      } catch (IOException e) {
        // Determine if we failed due to auth, or just failed
        if (cred.isInvalid()) {
          pm.deletePersistent(cred);
          startAuthFlow(resp, pm, oauthFlow);
        } else {
          throw e;
        }
      }
    } else {
      startAuthFlow(resp, pm, oauthFlow);
    }
  } finally {
    pm.close();
  }
}
 
Example 7
Source File: DelegateSentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
 * The role is global in the generic model, such as the role may be has more than one component
 * privileges, so delete role will remove all privileges related to it.
 */
@Override
public CommitContext dropRole(String component, String role, String requestor)
    throws SentryNoSuchObjectException {
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  role = toTrimmedLower(role);
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryRole.class);
    query.setFilter("this.roleName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    MSentryRole sentryRole = (MSentryRole) query.execute(role);
    if (sentryRole == null) {
      throw new SentryNoSuchObjectException("Role: " + role + " doesn't exist");
    } else {
      pm.retrieve(sentryRole);
      sentryRole.removeGMPrivileges();
      sentryRole.removePrivileges();
      pm.deletePersistent(sentryRole);
    }
    CommitContext commit = commitUpdateTransaction(pm);
    rollbackTransaction = false;
    return commit;
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 8
Source File: AbstractCallbackServlet.java    From google-oauth-java-client with Apache License 2.0 4 votes vote down vote up
@Override
protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  // Parse the token that will be used to look up the flow object
  String completionCode = req.getParameter(completionCodeQueryParam);
  String errorCode = req.getParameter(ERROR_PARAM);

  if ((completionCode == null || "".equals(completionCode))
      && (errorCode == null || "".equals(errorCode))) {
    resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    resp.getWriter().print("Must have a query parameter: " + completionCodeQueryParam);
    return;
  } else if (errorCode != null && !"".equals(errorCode)) {
    resp.sendRedirect(deniedRedirectUrl);
    return;
  }

  // Get a key for the logged in user to retrieve the flow
  String userId = getUserId();

  // Get flow from the data store
  PersistenceManager manager = pmf.getPersistenceManager();
  try {
    ThreeLeggedFlow flow = null;
    try {
      flow = manager.getObjectById(flowType, userId);
    } catch (JDOObjectNotFoundException e) {
      LOG.severe("Unable to locate flow by user: " + userId);
      resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
      resp.getWriter().print("Unable to find flow for user: " + userId);
      return;
    }

    flow.setHttpTransport(getHttpTransport());
    flow.setJsonFactory(getJsonFactory());

    // Complete the flow object with the token we got in our query parameters
    Credential c = flow.complete(completionCode);
    manager.makePersistent(c);
    manager.deletePersistent(flow);
    resp.sendRedirect(redirectUrl);
  } finally {
    manager.close();
  }
}
 
Example 9
Source File: TestSentryRole.java    From incubator-sentry with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeletePrivilegeAndRole() throws Exception {
  String roleName = "r1";
  //hive/impala privilege
  MSentryPrivilege hivePrivilege = new MSentryPrivilege();
  hivePrivilege.setServerName("hive.server1");
  hivePrivilege.setDbName("db1");
  hivePrivilege.setTableName("tb1");
  hivePrivilege.setPrivilegeScope("table");
  hivePrivilege.setAction("select");
  hivePrivilege.setURI(SentryStore.NULL_COL);
  hivePrivilege.setColumnName(SentryStore.NULL_COL);
  hivePrivilege.setGrantOption(true);

  //solr privilege
  MSentryGMPrivilege solrPrivilege = new MSentryGMPrivilege();
  solrPrivilege.setComponentName("solr");
  solrPrivilege.setServiceName("solr.server1");
  solrPrivilege.setAuthorizables(Arrays.asList(new Collection("c1")));
  solrPrivilege.setAction("query");
  solrPrivilege.setGrantOption(true);

  PersistenceManager pm = null;
  //create role
  pm = openTransaction();
  pm.makePersistent(new MSentryRole(roleName, System.currentTimeMillis()));
  commitTransaction(pm);

  //grant hivePrivilege and solrPrivilege to role
  pm = openTransaction();
  MSentryRole role = getMSentryRole(pm, roleName);
  hivePrivilege.appendRole(role);
  solrPrivilege.appendRole(role);
  pm.makePersistent(hivePrivilege);
  pm.makePersistent(solrPrivilege);
  commitTransaction(pm);

  //check
  pm = openTransaction();
  role = getMSentryRole(pm, roleName);
  pm.retrieve(role);
  assertEquals(1, role.getPrivileges().size());
  assertEquals(1, role.getGmPrivileges().size());
  commitTransaction(pm);

  //remove all privileges
  pm = openTransaction();
  role = getMSentryRole(pm, roleName);
  role.removeGMPrivileges();
  role.removePrivileges();
  pm.makePersistent(role);
  commitTransaction(pm);

  //check
  pm = openTransaction();
  role = getMSentryRole(pm, roleName);
  pm.retrieve(role);
  assertEquals(0, role.getPrivileges().size());
  assertEquals(0, role.getGmPrivileges().size());
  commitTransaction(pm);

  //delete role
  pm = openTransaction();
  role = getMSentryRole(pm, roleName);
  pm.deletePersistent(role);
  commitTransaction(pm);

  //check
  pm = openTransaction();
  role = getMSentryRole(pm, roleName);
  assertTrue(role == null);
  commitTransaction(pm);
}