Refactoring listDomains to the new API framework. The search criteria are now retrieved from the command itself rather than creating an intermediate Criteria object first.

This commit is contained in:
Kris McQueen 2010-08-27 16:10:13 -07:00
parent e55e6c283f
commit 9eeabb701a
3 changed files with 47 additions and 93 deletions

View File

@ -20,35 +20,21 @@ package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseListCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.response.DomainResponse;
import com.cloud.domain.DomainVO;
import com.cloud.server.Criteria;
import com.cloud.user.Account;
import com.cloud.utils.Pair;
public class ListDomainsCmd extends BaseCmd {
import com.cloud.serializer.SerializerHelper;
@Implementation(method="searchForDomains")
public class ListDomainsCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListDomainsCmd.class.getName());
private static final String s_name = "listdomainsresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_LEVEL, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.KEYWORD, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -85,74 +71,28 @@ public class ListDomainsCmd extends BaseCmd {
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public String getName() {
return s_name;
}
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
Long domainId = (Long)params.get(BaseCmd.Properties.ID.getName());
String domainName = (String)params.get(BaseCmd.Properties.NAME.getName());
Integer level = (Integer)params.get(BaseCmd.Properties.DOMAIN_LEVEL.getName());
String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName());
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
}
if (account != null) {
if (domainId != null) {
if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to list domains for domain id " + domainId + ", permission denied.");
}
} else {
domainId = account.getDomainId();
}
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<DomainVO> domains = (List<DomainVO>)getResponseObject();
List<DomainResponse> response = new ArrayList<DomainResponse>();
for (DomainVO domain : domains) {
DomainResponse domainResponse = new DomainResponse();
domainResponse.setDomainName(domain.getName());
domainResponse.setId(domain.getId());
domainResponse.setLevel(domain.getLevel());
domainResponse.setParentDomainId(domain.getParent());
// TODO: implement
// domainResponse.setParentDomainName(findDomainIdById(domain.getParent()).getName());
response.add(domainResponse);
}
Long startIndex = Long.valueOf(0);
int pageSizeNum = 50;
if (pageSize != null) {
pageSizeNum = pageSize.intValue();
}
if (page != null) {
int pageNum = page.intValue();
if (pageNum > 0) {
startIndex = Long.valueOf(pageSizeNum * (pageNum-1));
}
}
Criteria c = new Criteria("id", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum));
if (keyword != null) {
c.addCriteria(Criteria.KEYWORD, keyword);
} else {
c.addCriteria(Criteria.ID, domainId);
c.addCriteria(Criteria.NAME, domainName);
c.addCriteria(Criteria.LEVEL, level);
}
List<DomainVO> domains = getManagementServer().searchForDomains(c);
List<Pair<String, Object>> domainTags = new ArrayList<Pair<String, Object>>();
Object[] dTag = new Object[domains.size()];
int i = 0;
for (DomainVO domain : domains) {
List<Pair<String, Object>> domainData = new ArrayList<Pair<String, Object>>();
domainData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), Long.valueOf(domain.getId()).toString()));
domainData.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), domain.getName()));
domainData.add(new Pair<String, Object>(BaseCmd.Properties.LEVEL.getName(), domain.getLevel().toString()));
if (domain.getParent() != null){
domainData.add(new Pair<String, Object>(BaseCmd.Properties.PARENT_DOMAIN_ID.getName(), domain.getParent().toString()));
domainData.add(new Pair<String, Object>(BaseCmd.Properties.PARENT_DOMAIN_NAME.getName(),
getManagementServer().findDomainIdById(domain.getParent()).getName()));
}
dTag[i++] = domainData;
}
Pair<String, Object> domainTag = new Pair<String, Object>("domain", dTag);
domainTags.add(domainTag);
return domainTags;
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -37,6 +37,7 @@ import com.cloud.api.commands.ListCapacityCmd;
import com.cloud.api.commands.ListCfgsByCmd;
import com.cloud.api.commands.ListClustersCmd;
import com.cloud.api.commands.ListDiskOfferingsCmd;
import com.cloud.api.commands.ListDomainsCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.RebootSystemVmCmd;
@ -1355,10 +1356,10 @@ public interface ManagementServer {
/**
* Search for domains owned by the given domainId/domainName (those parameters are wrapped
* in a Criteria object.
* in a command object.
* @return list of domains owned by the given user
*/
List<DomainVO> searchForDomains(Criteria c);
List<DomainVO> searchForDomains(ListDomainsCmd c) throws PermissionDeniedException;
List<DomainVO> searchForDomainChildren(Criteria c);

View File

@ -76,6 +76,7 @@ import com.cloud.api.commands.ListCapacityCmd;
import com.cloud.api.commands.ListCfgsByCmd;
import com.cloud.api.commands.ListClustersCmd;
import com.cloud.api.commands.ListDiskOfferingsCmd;
import com.cloud.api.commands.ListDomainsCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd;
@ -6052,12 +6053,24 @@ public class ManagementServerImpl implements ManagementServer {
return _consoleProxyDao.findById(instanceId);
}
public List<DomainVO> searchForDomains(Criteria c) {
Filter searchFilter = new Filter(DomainVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
Long domainId = (Long) c.getCriteria(Criteria.ID);
String domainName = (String) c.getCriteria(Criteria.NAME);
Integer level = (Integer) c.getCriteria(Criteria.LEVEL);
Object keyword = c.getCriteria(Criteria.KEYWORD);
@Override
public List<DomainVO> searchForDomains(ListDomainsCmd cmd) throws PermissionDeniedException {
Long domainId = cmd.getId();
Account account = (Account)UserContext.current().getAccountObject();
if (account != null) {
if (domainId != null) {
if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
throw new PermissionDeniedException("Unable to list domains for domain id " + domainId + ", permission denied.");
}
} else {
domainId = account.getDomainId();
}
}
Filter searchFilter = new Filter(DomainVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
String domainName = cmd.getDomainName();
Integer level = cmd.getLevel();
Object keyword = cmd.getKeyword();
SearchBuilder<DomainVO> sb = _domainDao.createSearchBuilder();
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);