mirror of https://github.com/apache/cloudstack.git
Refactor listGuestOSCategories API to new framework.
This commit is contained in:
parent
5636503e39
commit
9abc658da0
|
|
@ -20,28 +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.GuestOSCategoryResponse;
|
||||
import com.cloud.serializer.SerializerHelper;
|
||||
import com.cloud.storage.GuestOSCategoryVO;
|
||||
import com.cloud.utils.Pair;
|
||||
|
||||
public class ListGuestOsCategoriesCmd extends BaseCmd {
|
||||
@Implementation(method="listGuestOSCategoriesByCriteria")
|
||||
public class ListGuestOsCategoriesCmd extends BaseListCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(ListIsosCmd.class.getName());
|
||||
|
||||
private static final String s_name = "listoscategoriesresponse";
|
||||
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.PAGE, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
|
|
@ -68,61 +61,20 @@ public class ListGuestOsCategoriesCmd 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) {
|
||||
|
||||
List<GuestOSCategoryVO> guestOSCategoryList = null;
|
||||
try
|
||||
{
|
||||
Long id = (Long)params.get(BaseCmd.Properties.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);
|
||||
}
|
||||
guestOSCategoryList = getManagementServer().listGuestOSCategoriesByCriteria(c);
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Exception listing guest OS categories", ex);
|
||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to list guest OS categories due to exception: " + ex.getMessage());
|
||||
@Override @SuppressWarnings("unchecked")
|
||||
public String getResponse() {
|
||||
List<GuestOSCategoryVO> osCategories = (List<GuestOSCategoryVO>)getResponseObject();
|
||||
|
||||
List<GuestOSCategoryResponse> response = new ArrayList<GuestOSCategoryResponse>();
|
||||
for (GuestOSCategoryVO osCategory : osCategories) {
|
||||
GuestOSCategoryResponse categoryResponse = new GuestOSCategoryResponse();
|
||||
categoryResponse.setId(osCategory.getId());
|
||||
categoryResponse.setName(osCategory.getName());
|
||||
|
||||
response.add(categoryResponse);
|
||||
}
|
||||
|
||||
Object[] tag = null;
|
||||
List<Pair<String, Object>> guestOSCategoryTags = new ArrayList<Pair<String, Object>>();
|
||||
if (guestOSCategoryList != null) {
|
||||
tag = new Object[guestOSCategoryList.size()];
|
||||
int i = 0;
|
||||
for (GuestOSCategoryVO guestOSCategory : guestOSCategoryList) {
|
||||
List<Pair<String, Object>> guestOSCategoryData = new ArrayList<Pair<String, Object>>();
|
||||
guestOSCategoryData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), guestOSCategory.getId().toString()));
|
||||
guestOSCategoryData.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), guestOSCategory.getName()));
|
||||
|
||||
tag[i++] = guestOSCategoryData;
|
||||
}
|
||||
} else {
|
||||
tag = new Object[0];
|
||||
}
|
||||
Pair<String, Object> guestOSCategoryTag = new Pair<String, Object>("oscategory", tag);
|
||||
guestOSCategoryTags.add(guestOSCategoryTag);
|
||||
return guestOSCategoryTags;
|
||||
|
||||
return SerializerHelper.toSerializedString(response);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
package com.cloud.api.response;
|
||||
|
||||
import com.cloud.api.ResponseObject;
|
||||
import com.cloud.serializer.Param;
|
||||
|
||||
public class GuestOSCategoryResponse implements ResponseObject {
|
||||
@Param(name="id")
|
||||
private Long id;
|
||||
|
||||
@Param(name="name")
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
|
@ -40,6 +40,7 @@ import com.cloud.api.commands.ListDiskOfferingsCmd;
|
|||
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.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
import com.cloud.api.commands.RebootSystemVmCmd;
|
||||
|
|
@ -1279,7 +1280,7 @@ public interface ManagementServer {
|
|||
* Obtains a list of all guest OS categories.
|
||||
* @return list of GuestOSCategories
|
||||
*/
|
||||
List<GuestOSCategoryVO> listGuestOSCategoriesByCriteria(Criteria c);
|
||||
List<GuestOSCategoryVO> listGuestOSCategoriesByCriteria(ListGuestOsCategoriesCmd cmd);
|
||||
|
||||
/**
|
||||
* Logs out a user
|
||||
|
|
|
|||
|
|
@ -79,6 +79,7 @@ import com.cloud.api.commands.ListDiskOfferingsCmd;
|
|||
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.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
import com.cloud.api.commands.PrepareForMaintenanceCmd;
|
||||
|
|
@ -5906,11 +5907,11 @@ public class ManagementServerImpl implements ManagementServer {
|
|||
|
||||
return _guestOSDao.search(sc, searchFilter);
|
||||
}
|
||||
|
||||
public List<GuestOSCategoryVO> listGuestOSCategoriesByCriteria(Criteria c)
|
||||
{
|
||||
Filter searchFilter = new Filter(GuestOSCategoryVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
|
||||
Long id = (Long) c.getCriteria(Criteria.ID);
|
||||
|
||||
@Override
|
||||
public List<GuestOSCategoryVO> listGuestOSCategoriesByCriteria(ListGuestOsCategoriesCmd cmd) {
|
||||
Filter searchFilter = new Filter(GuestOSCategoryVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
|
||||
Long id = cmd.getId();
|
||||
|
||||
SearchBuilder<GuestOSCategoryVO> sb = _guestOSCategoryDao.createSearchBuilder();
|
||||
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
|
||||
|
|
|
|||
Loading…
Reference in New Issue