Refactor listOSTypes API to new framework.

This commit is contained in:
Kris McQueen 2010-08-30 15:15:19 -07:00
parent 9abc658da0
commit b797313ac0
4 changed files with 67 additions and 83 deletions

View File

@ -20,29 +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.server.Criteria;
import com.cloud.api.response.GuestOSResponse;
import com.cloud.serializer.SerializerHelper;
import com.cloud.storage.GuestOSVO;
import com.cloud.utils.Pair;
public class ListGuestOsCmd extends BaseCmd {
@Implementation(method="listGuestOSByCriteria")
public class ListGuestOsCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListIsosCmd.class.getName());
private static final String s_name = "listostypesresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.OS_CATEGORY_ID, 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 /////////////////////
@ -76,69 +68,21 @@ public class ListGuestOsCmd extends BaseCmd {
public String getName() {
return s_name;
}
@Override
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<GuestOSVO> guestOSList = (List<GuestOSVO>)getResponseObject();
List<GuestOSVO> guestOSList = null;
try
{
Long id = (Long)params.get(BaseCmd.Properties.ID.getName());
Long osCategoryId = (Long)params.get(BaseCmd.Properties.OS_CATEGORY_ID.getName());
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
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 (id != null) {
c.addCriteria(Criteria.ID, id);
}
if (osCategoryId != null) {
c.addCriteria(Criteria.OSCATEGORYID, osCategoryId);
}
guestOSList = getManagementServer().listGuestOSByCriteria(c);
}
catch (Exception ex)
{
s_logger.error("Exception listing guest OS", ex);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to list guest OS due to exception: " + ex.getMessage());
}
Object[] tag = null;
List<Pair<String, Object>> guestOSTags = new ArrayList<Pair<String, Object>>();
if (guestOSList != null) {
tag = new Object[guestOSList.size()];
int i = 0;
List<GuestOSResponse> response = new ArrayList<GuestOSResponse>();
for (GuestOSVO guestOS : guestOSList) {
List<Pair<String, Object>> guestOSData = new ArrayList<Pair<String, Object>>();
guestOSData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), guestOS.getId().toString()));
guestOSData.add(new Pair<String, Object>(BaseCmd.Properties.OS_CATEGORY_ID.getName(), guestOS.getCategoryId()));
guestOSData.add(new Pair<String, Object>(BaseCmd.Properties.DESCRIPTION.getName(), guestOS.getDisplayName()));
GuestOSResponse guestOSResponse = new GuestOSResponse();
guestOSResponse.setDescription(guestOS.getDisplayName());
guestOSResponse.setId(guestOS.getId());
guestOSResponse.setOsCategoryId(guestOS.getCategoryId());
tag[i++] = guestOSData;
response.add(guestOSResponse);
}
} else {
tag = new Object[0];
}
Pair<String, Object> guestOSTag = new Pair<String, Object>("ostype", tag);
guestOSTags.add(guestOSTag);
return guestOSTags;
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -0,0 +1,39 @@
package com.cloud.api.response;
import com.cloud.api.ResponseObject;
import com.cloud.serializer.Param;
public class GuestOSResponse implements ResponseObject {
@Param(name="id")
private Long id;
@Param(name="oscategoryid")
private Long osCategoryId;
@Param(name="description")
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getOsCategoryId() {
return osCategoryId;
}
public void setOsCategoryId(Long osCategoryId) {
this.osCategoryId = osCategoryId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -41,6 +41,7 @@ import com.cloud.api.commands.ListDomainChildrenCmd;
import com.cloud.api.commands.ListDomainsCmd;
import com.cloud.api.commands.ListEventsCmd;
import com.cloud.api.commands.ListGuestOsCategoriesCmd;
import com.cloud.api.commands.ListGuestOsCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.RebootSystemVmCmd;
@ -1274,7 +1275,7 @@ public interface ManagementServer {
* Obtains a list of all guest OS.
* @return list of GuestOS
*/
List<GuestOSVO> listGuestOSByCriteria(Criteria c);
List<GuestOSVO> listGuestOSByCriteria(ListGuestOsCmd cmd);
/**
* Obtains a list of all guest OS categories.

View File

@ -80,6 +80,7 @@ import com.cloud.api.commands.ListDomainChildrenCmd;
import com.cloud.api.commands.ListDomainsCmd;
import com.cloud.api.commands.ListEventsCmd;
import com.cloud.api.commands.ListGuestOsCategoriesCmd;
import com.cloud.api.commands.ListGuestOsCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
import com.cloud.api.commands.PrepareForMaintenanceCmd;
@ -5884,12 +5885,11 @@ public class ManagementServerImpl implements ManagementServer {
return _templateDao.listAll();
}
public List<GuestOSVO> listGuestOSByCriteria(Criteria c)
{
Filter searchFilter = new Filter(GuestOSVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
Long id = (Long) c.getCriteria(Criteria.ID);
Long osCategoryId = (Long) c.getCriteria(Criteria.OSCATEGORYID);
@Override
public List<GuestOSVO> listGuestOSByCriteria(ListGuestOsCmd cmd) {
Filter searchFilter = new Filter(GuestOSVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
Long id = cmd.getId();
Long osCategoryId = cmd.getOsCategoryId();
SearchBuilder<GuestOSVO> sb = _guestOSDao.createSearchBuilder();
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);