Github user jburwell commented on a diff in the pull request:

    https://github.com/apache/cloudstack/pull/689#discussion_r37628668
  
    --- Diff: 
plugins/database/quota/src/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java
 ---
    @@ -0,0 +1,419 @@
    +//Licensed to the Apache Software Foundation (ASF) under one
    +//or more contributor license agreements.  See the NOTICE file
    +//distributed with this work for additional information
    +//regarding copyright ownership.  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.
    +package org.apache.cloudstack.api.response;
    +
    +import com.cloud.exception.InvalidParameterValueException;
    +import com.cloud.user.Account;
    +import com.cloud.user.AccountVO;
    +import com.cloud.user.User;
    +import com.cloud.user.dao.AccountDao;
    +import com.cloud.user.dao.UserDao;
    +import com.cloud.utils.db.TransactionLegacy;
    +
    +import org.apache.cloudstack.api.command.QuotaBalanceCmd;
    +import org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd;
    +import org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd;
    +import org.apache.cloudstack.api.command.QuotaStatementCmd;
    +import org.apache.cloudstack.api.command.QuotaTariffListCmd;
    +import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd;
    +import org.apache.cloudstack.quota.QuotaService;
    +import org.apache.cloudstack.quota.constant.QuotaConfig;
    +import org.apache.cloudstack.quota.constant.QuotaTypes;
    +import org.apache.cloudstack.quota.dao.QuotaBalanceDao;
    +import org.apache.cloudstack.quota.dao.QuotaCreditsDao;
    +import org.apache.cloudstack.quota.dao.QuotaEmailTemplatesDao;
    +import org.apache.cloudstack.quota.dao.QuotaTariffDao;
    +import org.apache.cloudstack.quota.vo.QuotaBalanceVO;
    +import org.apache.cloudstack.quota.vo.QuotaCreditsVO;
    +import org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO;
    +import org.apache.cloudstack.quota.vo.QuotaTariffVO;
    +import org.apache.cloudstack.quota.vo.QuotaUsageVO;
    +import org.apache.cloudstack.region.RegionManager;
    +import org.apache.commons.lang.StringEscapeUtils;
    +import org.apache.log4j.Logger;
    +import org.springframework.stereotype.Component;
    +
    +import javax.ejb.Local;
    +import javax.inject.Inject;
    +
    +import java.math.BigDecimal;
    +import java.util.ArrayList;
    +import java.util.Calendar;
    +import java.util.Collections;
    +import java.util.Comparator;
    +import java.util.Date;
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.List;
    +
    +@Component
    +@Local(value = QuotaResponseBuilderImpl.class)
    +public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
    +    private static final Logger s_logger = 
Logger.getLogger(QuotaResponseBuilderImpl.class.getName());
    +
    +    @Inject
    +    private QuotaTariffDao _quotaTariffDao;
    +    @Inject
    +    private QuotaBalanceDao _quotaBalanceDao;
    +    @Inject
    +    private QuotaCreditsDao _quotaCreditsDao;
    +    @Inject
    +    private QuotaEmailTemplatesDao _quotaEmailTemplateDao;
    +
    +    @Inject
    +    private UserDao _userDao;
    +    @Inject
    +    private QuotaService _quotaService;
    +    @Inject
    +    AccountDao _accountDao;
    +    @Inject
    +    private RegionManager _regionMgr;
    +
    +    @Override
    +    public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO 
tariff) {
    +        final QuotaTariffResponse response = new QuotaTariffResponse();
    +        response.setUsageType(tariff.getUsageType());
    +        response.setUsageName(tariff.getUsageName());
    +        response.setUsageUnit(tariff.getUsageUnit());
    +        response.setUsageDiscriminator(tariff.getUsageDiscriminator());
    +        response.setTariffValue(tariff.getCurrencyValue());
    +        response.setEffectiveOn(tariff.getEffectiveOn());
    +        response.setDescription(tariff.getDescription());
    +        response.setCurrency(QuotaConfig.QuotaCurrencySymbol.value());
    +        return response;
    +    }
    +
    +    @Override
    +    public QuotaBalanceResponse 
createQuotaBalanceResponse(List<QuotaBalanceVO> quotaBalance, Date startDate, 
Date endDate) {
    +        if (quotaBalance.size() == 0) {
    +            new InvalidParameterValueException("The request period does 
not contain balance entries.");
    +        }
    +        Collections.sort(quotaBalance, new Comparator<QuotaBalanceVO>() {
    +            public int compare(QuotaBalanceVO o1, QuotaBalanceVO o2) {
    +                return o2.getUpdatedOn().compareTo(o1.getUpdatedOn()); // 
desc
    +            }
    +        });
    +
    +        int quota_activity = quotaBalance.size();
    +        QuotaBalanceResponse resp = new QuotaBalanceResponse();
    +        BigDecimal lastCredits = new BigDecimal(0);
    +        boolean consecutive = true;
    +        for (Iterator<QuotaBalanceVO> it = quotaBalance.iterator(); 
it.hasNext();) {
    +            QuotaBalanceVO entry = it.next();
    +            s_logger.info("createQuotaBalanceResponse: Date=" + 
entry.getUpdatedOn().toGMTString() + " balance=" + entry.getCreditBalance() + " 
credit=" + entry.getCreditsId());
    +            if (entry.getCreditsId() > 0) {
    +                if (consecutive) {
    +                    lastCredits = 
lastCredits.add(entry.getCreditBalance());
    +                }
    +                resp.addCredits(entry);
    +                it.remove();
    --- End diff --
    
    @abhinandanprateek this issue is not whether or not it is the correct way 
to modify a list, but whether or not that list should be modified.  
``quotaBalance`` is passed into the method.  Therefore, ``it.remove()`` is 
modifying a list referenced by the caller -- causing unexpected side effects.  
To avoid these effects, consider copying the contents of ``quotaBalance`` to a 
new list (e.g. ``final List<QuotaBalanceVO> updatedBalances = new 
ArrayList<>(quotaBalance)``) and modifying the copied list.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to