Search in sources :

Example 1 with StepicUser

use of com.jetbrains.edu.learning.stepic.StepicUser in project intellij-community by JetBrains.

the class StudyProjectGenerator method getCourseInfo.

/**
   * Parses course json meta file and finds course name
   *
   * @return information about course or null if course file is invalid
   */
@Nullable
private static CourseInfo getCourseInfo(File courseFile) {
    if (courseFile.isDirectory()) {
        File[] courseFiles = courseFile.listFiles((dir, name) -> name.equals(EduNames.COURSE_META_FILE));
        if (courseFiles == null || courseFiles.length != 1) {
            LOG.info("More than one or without course files");
            return null;
        }
        courseFile = courseFiles[0];
    }
    CourseInfo courseInfo = null;
    BufferedReader reader = null;
    try {
        if (courseFile.getName().equals(EduNames.COURSE_META_FILE)) {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(courseFile), "UTF-8"));
            JsonReader r = new JsonReader(reader);
            JsonParser parser = new JsonParser();
            JsonElement el = parser.parse(r);
            String courseName = el.getAsJsonObject().get(COURSE_NAME_ATTRIBUTE).getAsString();
            String courseDescription = el.getAsJsonObject().get(COURSE_DESCRIPTION).getAsString();
            JsonArray courseAuthors = el.getAsJsonObject().get(AUTHOR_ATTRIBUTE).getAsJsonArray();
            String language = el.getAsJsonObject().get(LANGUAGE_ATTRIBUTE).getAsString();
            courseInfo = new CourseInfo();
            courseInfo.setName(courseName);
            courseInfo.setDescription(courseDescription);
            courseInfo.setType("pycharm " + language);
            final ArrayList<StepicUser> authors = new ArrayList<>();
            for (JsonElement author : courseAuthors) {
                final JsonObject authorAsJsonObject = author.getAsJsonObject();
                final StepicUser stepicUser = new StepicUser();
                stepicUser.setFirstName(authorAsJsonObject.get("first_name").getAsString());
                stepicUser.setLastName(authorAsJsonObject.get("last_name").getAsString());
                authors.add(stepicUser);
            }
            courseInfo.setAuthors(authors);
        }
    } catch (Exception e) {
    //error will be shown in UI
    } finally {
        StudyUtils.closeSilently(reader);
    }
    return courseInfo;
}
Also used : CourseInfo(com.jetbrains.edu.learning.courseFormat.CourseInfo) JsonReader(com.google.gson.stream.JsonReader) StepicUser(com.jetbrains.edu.learning.stepic.StepicUser) VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) PsiFile(com.intellij.psi.PsiFile) NewVirtualFile(com.intellij.openapi.vfs.newvfs.NewVirtualFile) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with StepicUser

use of com.jetbrains.edu.learning.stepic.StepicUser in project intellij-community by JetBrains.

the class Course method setAuthorsAsString.

@Transient
public void setAuthorsAsString(String[] authors) {
    this.authors = new ArrayList<>();
    for (String name : authors) {
        final List<String> firstLast = StringUtil.split(name, " ");
        if (!firstLast.isEmpty()) {
            final StepicUser stepicUser = new StepicUser();
            stepicUser.setFirstName(firstLast.remove(0));
            if (firstLast.size() > 0) {
                stepicUser.setLastName(StringUtil.join(firstLast, " "));
            }
            this.authors.add(stepicUser);
        }
    }
}
Also used : StepicUser(com.jetbrains.edu.learning.stepic.StepicUser) Transient(com.intellij.util.xmlb.annotations.Transient)

Aggregations

StepicUser (com.jetbrains.edu.learning.stepic.StepicUser)2 JsonReader (com.google.gson.stream.JsonReader)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 NewVirtualFile (com.intellij.openapi.vfs.newvfs.NewVirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 Transient (com.intellij.util.xmlb.annotations.Transient)1 CourseInfo (com.jetbrains.edu.learning.courseFormat.CourseInfo)1 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)1 Nullable (org.jetbrains.annotations.Nullable)1