Search code examples
javaoracleformatlocalenavicat

translate month name response


I have a table named CREATED_AT I wrote a query and receive the response like this : Score D 27 01 17 02 80 03 55 06

this is my query :

SELECT SUM(AMOUNT) as scores,to_char(CREATED_AT, 'MONTH') as  d  
FROM C_SCORES 
WHERE USER_ID =201 and to_char(CREATED_AT, 'YYYY') ='1398' 
GROUP BY to_char(CREATED_AT, 'MONTH');

the entity code is :

    public class ScoreCategorizeWithMonth {

        private int Scores;
        private int month;
    private String monthfa;


 public String getMonthfa() {
        switch (month) {
            case 1:
                monthfa = "فروردین";
                break;
            case 2:
                monthfa = "اردیبهشت";
                break;
            case 3:
                monthfa = "خرداد";
                break;
            case 4:
                monthfa = "تیر";
                break;
            case 5:
                monthfa = "مرداد";
                break;
            case 6:
                monthfa = "شهریور";
                break;
            case 7:
                monthfa = "مهر";
                break;
            case 8:
                monthfa = "آبان";
                break;
            case 9:
                monthfa = "آذر";
                break;
            case 10:
                monthfa = "دی";
                break;
            case 11:
                monthfa = "بهمن";
                break;
            case 12:
                monthfa = "اسفند";
            default:
                monthfa = "";
        }



        return monthfa;
    }

    }

this code is in the controller :

 @GetMapping(value = "score-Categorize-months")
    public List<ScoreCategorizeWithMonth> scoreCategorizedWithMonths()
    {
        return   iScoresSrv.scoreCategorizeWithMonths(userInfo.getUserId());
    }

my question is :

2- my response is the months that exist in database.how to show months even its not in the database

 {
    "scores": 27,
    "month": 1,
    "monthfa": "فروردین"
  },
  {
    "scores": 17,
    "month": 2,
    "monthfa": "اردیبهشت"
  },
  {
    "scores": 84,
    "month": 3,
    "monthfa": "خرداد"
  },
  {
    "scores": 55,
    "month": 6,
    "monthfa": "شهریور"
  } 

I want to show also the months of 4..5 with score=0

thanks


Solution

  • I just add sume code in java to fill the indexes that are invalid . it was so easy to solve . but I was a bit confused and none of the answers didnt help me

     public List<ScoreCategorizedWithMonth> scoreCategorizeWithMonths(Long userId) {
            List<ScoreCategorizedWithMonth> monthCategory = scoreDao.scoreCategorizeWithMonths(userId);
            List<ScoreCategorizedWithMonth> result = new ArrayList<>();
            for (int i=1; i<=12;i++)
            {
                ScoreCategorizedWithMonth scoreCategorizedWithMonth = checkMonthCatIsExist(i, monthCategory);
                result.add(scoreCategorizedWithMonth);
            }
            return result;
        }
    
        private ScoreCategorizedWithMonth checkMonthCatIsExist(int month, List<ScoreCategorizedWithMonth> monthCategory) {
            for (ScoreCategorizedWithMonth cat:monthCategory) {
                if(cat.getMonth() == month){
                    return cat;
                }
            }
            ScoreCategorizedWithMonth temp= new  ScoreCategorizedWithMonth();
            temp.setMonth(month);
            temp.setScores(0);
            return temp;
        }