public Quiz buildQuiz(Quiz q, TreeMap<SubjectType, Integer> subjects)
    {
        Random random = new Random();
        for(SubjectType s : subjects.keySet())
        {
            int[] selections = new int[subjects.get(s)];
            for(int i = 0; i < selections.length; i++)
                selections[i] = -1;
            Query query = em.createQuery("SELECT q FROM Question q where q.subject like '"+s.getLabel()+"'");
            List<Question> results = query.getResultList();
            if(results.size() >= selections.length)
            {
                for(int i = 0; i < selections.length; i++)
                {
                    int n = 0;
                    boolean contains = false;
                    do
                    {
                        contains = false;
                        n = random.nextInt(results.size());
                        for(int j = 0; j < selections.length; j++)
                            if(selections[j] == n)
                            {
                                contains = true;
                                break;
                            }
                    }while(contains);

                    selections[i] = n;
                }
                for(int i : selections)
                {
                    q.addQuestion(results.get(i));
                }
            }
            else
            {
                for(int i = 0; i < results.size(); i++)
                {
                    q.addQuestion(results.get(i));
                }
            }
        }
        q.setIsTemporary(true);
     //   em.persist(q);
        return q;
    }