Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
Removing redundant "appinfos" collection, work in progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Johnson committed Mar 3, 2015
1 parent 67cb719 commit 485f578
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 103 deletions.
Expand Up @@ -137,14 +137,14 @@ public ManagerCache getManagerCache() {

private void init() {

EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);

try {
if ( em.getApplication() == null ) {
logger.info("Creating system application");
Map sysAppProps = new HashMap<String, Object>();
sysAppProps.put(PROPERTY_NAME, "systemapp");
em.create(CpNamingUtils.SYSTEM_APP_ID, TYPE_APPLICATION, sysAppProps);
logger.info("Creating management application");
Map mgmtAppProps = new HashMap<String, Object>();
mgmtAppProps.put(PROPERTY_NAME, "systemapp");
em.create(CpNamingUtils.MANAGEMENT_APPLICATION_ID, TYPE_APPLICATION, mgmtAppProps);
em.getApplication();
em.createIndex();
em.refreshIndex();
Expand All @@ -156,18 +156,6 @@ private void init() {
}


// public ManagerCache getManagerCache() {
//
// if ( managerCache == null ) {
// managerCache = injector.getInstance( ManagerCache.class );
//
// dataMigrationManager = injector.getInstance( DataMigrationManager.class );
// }
// return managerCache;
// }



@Override
public EntityManager getEntityManager(UUID applicationId) {
try {
Expand Down Expand Up @@ -233,7 +221,7 @@ public UUID initializeApplication( String organizationName, UUID applicationId,
Map<String, Object> properties ) throws Exception {


EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);

final String appName = buildAppName( organizationName, name );

Expand Down Expand Up @@ -274,11 +262,11 @@ public UUID initializeApplication( String organizationName, UUID applicationId,
put( "organizationUuid", orgId );
}};

try{
em.create( "appinfo", appInfoMap );
}catch(DuplicateUniquePropertyExistsException e){
throw new ApplicationAlreadyExistsException( appName );
}
try {
em.create(CpNamingUtils.APPLICATION_INFO, appInfoMap);
} catch (DuplicateUniquePropertyExistsException e) {
throw new ApplicationAlreadyExistsException(appName);
}
em.refreshIndex();

// create application entity
Expand Down Expand Up @@ -310,17 +298,15 @@ public UUID initializeApplication( String organizationName, UUID applicationId,
@Override
public void deleteApplication(UUID applicationId) throws Exception {

//throw new UnsupportedOperationException("Delete application not supported");

// remove old appinfo Entity, which is in the System App's appinfos collection
EntityManager em = getEntityManager(CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager( this.getManagementAppId() );
Query q = Query.fromQL(String.format("select * where applicationUuid = '%s'", applicationId.toString()));
Results results = em.searchCollection( em.getApplicationRef(), "appinfos", q);
Results results = em.searchCollection( em.getApplicationRef(), CpNamingUtils.APPLICATION_INFOS, q );
Entity appToDelete = results.getEntity();
em.delete( appToDelete );

// create new Entity in deleted_appinfos collection, with same UUID and properties as deleted appinfo
em.create( "deleted_appinfo", appToDelete.getProperties() );
em.create( CpNamingUtils.DELETED_APPINFO, appToDelete.getProperties() );

em.refreshIndex();

Expand All @@ -335,9 +321,9 @@ public void deleteApplication(UUID applicationId) throws Exception {
public void restoreApplication(UUID applicationId) throws Exception {

// remove old delete_appinfos Entity
EntityManager em = getEntityManager(CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
Query q = Query.fromQL(String.format("select * where applicationUuid = '%s'", applicationId.toString()));
Results results = em.searchCollection( em.getApplicationRef(), "deleted_appinfos", q);
Results results = em.searchCollection(em.getApplicationRef(), CpNamingUtils.DELETED_APPINFOS, q);
Entity appToRestore = results.getEntity();

if ( appToRestore == null ) {
Expand All @@ -349,8 +335,7 @@ public void restoreApplication(UUID applicationId) throws Exception {
// restore entity in appinfo collection
Map<String, Object> appProps = appToRestore.getProperties();
appProps.remove("uuid");
appProps.put("type", "appinfo");
Entity restoredApp = em.create("appinfo", appToRestore.getProperties());
Entity restoredApp = em.create(CpNamingUtils.APPLICATION_INFO, appToRestore.getProperties());

em.refreshIndex();

Expand All @@ -377,10 +362,8 @@ public UUID importApplication(
public UUID lookupOrganization( String name ) throws Exception {
init();


// Query q = Query.fromQL(PROPERTY_NAME + " = '" + name + "'");
EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID );

// Query q = Query.fromQL(PROPERTY_NAME + " = '" + name + "'");
EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID );

final EntityRef alias = em.getAlias( "organizations", name );

Expand All @@ -395,13 +378,6 @@ public UUID lookupOrganization( String name ) throws Exception {
}

return entity.getUuid();
// Results results = em.searchCollection( em.getApplicationRef(), "organizations", q );
//
// if ( results.isEmpty() ) {
// return null;
// }
//
// return results.iterator().next().getUuid();
}


Expand All @@ -425,9 +401,9 @@ public UUID lookupApplication( String name ) throws Exception {

Query q = Query.fromQL( PROPERTY_NAME + " = '" + name + "'");

EntityManager em = getEntityManager(CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);

Results results = em.searchCollection( em.getApplicationRef(), "appinfos", q);
Results results = em.searchCollection( em.getApplicationRef(), CpNamingUtils.APPLICATION_INFOS, q);

if ( results.isEmpty() ) {
return null;
Expand Down Expand Up @@ -461,10 +437,10 @@ public Map<String, UUID> getApplications(boolean deleted) throws Exception {

Map<String, UUID> appMap = new HashMap<String, UUID>();

ApplicationScope appScope = CpNamingUtils.getApplicationScope( CpNamingUtils.SYSTEM_APP_ID );
ApplicationScope appScope = CpNamingUtils.getApplicationScope( CpNamingUtils.MANAGEMENT_APPLICATION_ID );
GraphManager gm = managerCache.getGraphManager(appScope);

EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);
Application app = em.getApplication();
Id fromEntityId = new SimpleId( app.getUuid(), app.getType() );

Expand All @@ -474,8 +450,8 @@ public Map<String, UUID> getApplications(boolean deleted) throws Exception {
edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.DELETED_APPINFOS);
scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(CpNamingUtils.DELETED_APPINFOS);
} else {
edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPINFOS);
scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(CpNamingUtils.APPINFOS);
edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS );
scopeName = CpNamingUtils.getCollectionScopeNameFromCollectionName(CpNamingUtils.APPLICATION_INFOS);
}

logger.debug("getApplications(): Loading edges of edgeType {} from {}:{}",
Expand Down Expand Up @@ -531,7 +507,7 @@ public Map<String, String> getServiceProperties() {

Map<String, String> props = new HashMap<String,String>();

EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);
Query q = Query.fromQL("select *");
Results results = null;
try {
Expand All @@ -556,7 +532,7 @@ public Map<String, String> getServiceProperties() {
@Override
public boolean updateServiceProperties(Map<String, String> properties) {

EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);
Query q = Query.fromQL("select *");
Results results = null;
try {
Expand Down Expand Up @@ -605,7 +581,7 @@ public boolean setServiceProperty(final String name, final String value) {
@Override
public boolean deleteServiceProperty(String name) {

EntityManager em = getEntityManager( CpNamingUtils.SYSTEM_APP_ID);
EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID);


Query q = Query.fromQL("select *");
Expand Down Expand Up @@ -719,8 +695,7 @@ private void maybeCreateIndexes() {

private List<EntityIndex> getManagementIndexes() {

return Arrays.asList( managerCache.getEntityIndex(
new ApplicationScopeImpl( new SimpleId( CpNamingUtils.SYSTEM_APP_ID, "application" ) ) ),
return Arrays.asList(

// management app
managerCache
Expand Down Expand Up @@ -750,7 +725,6 @@ public void rebuildAllIndexes( ProgressObserver po ) throws Exception {

@Override
public void rebuildInternalIndexes( ProgressObserver po ) throws Exception {
rebuildApplicationIndexes( CpNamingUtils.SYSTEM_APP_ID, po);
rebuildApplicationIndexes( CpNamingUtils.MANAGEMENT_APPLICATION_ID, po );
rebuildApplicationIndexes( CpNamingUtils.DEFAULT_APPLICATION_ID, po );
}
Expand Down Expand Up @@ -833,8 +807,8 @@ public Health getEntityStoreHealth() {
// could use any collection scope here, does not matter
EntityCollectionManager ecm = getManagerCache().getEntityCollectionManager(
new CollectionScopeImpl(
new SimpleId( CpNamingUtils.SYSTEM_APP_ID, "application"),
new SimpleId( CpNamingUtils.SYSTEM_APP_ID, "application"),
new SimpleId( CpNamingUtils.MANAGEMENT_APPLICATION_ID, "application"),
new SimpleId( CpNamingUtils.MANAGEMENT_APPLICATION_ID, "application"),
"dummy"
));

Expand Down
Expand Up @@ -63,25 +63,24 @@ public static Observable<Id> getAllApplicationIds( final ManagerCache managerCac


final Observable<Id> systemIds = Observable.from( Arrays
.asList( generateApplicationId( CpNamingUtils.DEFAULT_APPLICATION_ID ),
generateApplicationId( CpNamingUtils.MANAGEMENT_APPLICATION_ID ),
generateApplicationId( CpNamingUtils.SYSTEM_APP_ID ) ) );
.asList( generateApplicationId( CpNamingUtils.DEFAULT_APPLICATION_ID ),
generateApplicationId( CpNamingUtils.MANAGEMENT_APPLICATION_ID ) ) );


final ApplicationScope appScope = getApplicationScope( CpNamingUtils.SYSTEM_APP_ID );
final ApplicationScope appScope = getApplicationScope( CpNamingUtils.MANAGEMENT_APPLICATION_ID );

final CollectionScope appInfoCollectionScope =
new CollectionScopeImpl( appScope.getApplication(), appScope.getApplication(),
CpNamingUtils.getCollectionScopeNameFromCollectionName( CpNamingUtils.APPINFOS ) );
new CollectionScopeImpl( appScope.getApplication(), appScope.getApplication(),
CpNamingUtils.getCollectionScopeNameFromCollectionName( CpNamingUtils.APPLICATION_INFOS) );

final EntityCollectionManager collectionManager =
managerCache.getEntityCollectionManager( appInfoCollectionScope );
managerCache.getEntityCollectionManager( appInfoCollectionScope );


final GraphManager gm = managerCache.getGraphManager( appScope );


String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( CpNamingUtils.APPINFOS );
String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( CpNamingUtils.APPLICATION_INFOS );

Id rootAppId = appScope.getApplication();

Expand Down
Expand Up @@ -38,34 +38,31 @@
*/
public class CpNamingUtils {

/**
* TODO: Why do we have 3? Can we merge this into a single management app? It would make administration much
* easier and cleaner on the ES side
*
*/
// TODO: Why do we have three internal apps? Can we merge this into a single management app?
// It would make administration much easier and cleaner on the ES side

/**
* Edge types for collection suffix
*/
/** Edge types for collection suffix */
public static final String EDGE_COLL_SUFFIX = "zzzcollzzz";

/**
* Edge types for connection suffix
*/
/** Edge types for connection suffix */
public static final String EDGE_CONN_SUFFIX = "zzzconnzzz";
/** The System Application where we store app and org metadata */
public static final UUID SYSTEM_APP_ID =
UUID.fromString("b6768a08-b5d5-11e3-a495-10ddb1de66c3");

/** App where we store management info */
public static final UUID MANAGEMENT_APPLICATION_ID =
UUID.fromString("b6768a08-b5d5-11e3-a495-11ddb1de66c8");

/** TODO Do we need this in two-dot-o? */
public static final UUID DEFAULT_APPLICATION_ID =
UUID.fromString("b6768a08-b5d5-11e3-a495-11ddb1de66c9");

/**
* The app infos entity object type. This holds the app name, appId, and org name
*/
public static final String APPINFOS = "appinfos";
public static final String APPLICATION_INFO = "appinfo";

public static final String APPLICATION_INFOS = "appinfos";

public static final String DELETED_APPINFO = "deleted_appinfo";

public static final String DELETED_APPINFOS = "deleted_appinfos";

Expand Down
Expand Up @@ -56,7 +56,6 @@ public void getAllApplications() throws Exception {
final Set<UUID> applicationIds = new HashSet<UUID>() {{
add( CpNamingUtils.DEFAULT_APPLICATION_ID );
add( CpNamingUtils.MANAGEMENT_APPLICATION_ID );
add( CpNamingUtils.SYSTEM_APP_ID );
add( createdApplication.getUuid() );
}};

Expand Down
@@ -0,0 +1,58 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. The ASF licenses this file to You
* * under the Apache License, Version 2.0 (the "License"); you may not
* * use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License. For additional information regarding
* * copyright in this work, please see the NOTICE file in the top level
* * directory of this distribution.
*
*/

package org.apache.usergrid.rest.test.resource2point0.endpoints.mgmt;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.usergrid.rest.test.resource2point0.endpoints.NamedResource;
import org.apache.usergrid.rest.test.resource2point0.endpoints.UrlResource;
import org.apache.usergrid.rest.test.resource2point0.state.ClientContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.io.StringReader;


/**
* Management end-point for getting list of applications in organization.
*/
public class ApplicationsResource extends NamedResource {
private static final Logger logger = LoggerFactory.getLogger(ApplicationsResource.class);
ObjectMapper mapper = new ObjectMapper();

public ApplicationsResource( final ClientContext context, final UrlResource parent ) {
super( "apps", context, parent );
}

public OrganizationApplicationResponse getOrganizationApplications() throws IOException {

String responseString = this.getResource()
.queryParam( "access_token", context.getToken().getAccessToken() )
.type(MediaType.APPLICATION_JSON)
.get(String.class);

logger.info("Response: " + responseString);

return mapper.readValue(
new StringReader(responseString), OrganizationApplicationResponse.class);
}
}

0 comments on commit 485f578

Please sign in to comment.