2 * Copyright 2000-2013 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.edu.coursecreator;
18 import com.intellij.openapi.components.PersistentStateComponent;
19 import com.intellij.openapi.components.ServiceManager;
20 import com.intellij.openapi.components.State;
21 import com.intellij.openapi.components.Storage;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.editor.Document;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.util.io.FileUtil;
26 import com.intellij.openapi.util.io.FileUtilRt;
27 import com.intellij.util.xmlb.XmlSerializer;
28 import com.intellij.util.xmlb.annotations.Transient;
29 import com.jetbrains.edu.learning.StudyUtils;
30 import com.jetbrains.edu.learning.courseFormat.Course;
31 import org.jdom.Element;
32 import org.jetbrains.annotations.NotNull;
36 import static com.jetbrains.edu.learning.StudySerializationUtils.*;
37 import static com.jetbrains.edu.learning.StudySerializationUtils.Xml.*;
40 * @deprecated since version 3
42 @State(name = "CCProjectService", storages = @Storage("course_service.xml"))
43 public class CCProjectService implements PersistentStateComponent<Element> {
44 private static final Logger LOG = Logger.getInstance(CCProjectService.class);
45 private Course myCourse;
46 @Transient private final Project myProject;
48 public CCProjectService() {
52 public CCProjectService(Project project) {
56 public Course getCourse() {
60 public void setCourse(Course course) {
65 public Element getState() {
66 if (myCourse == null) {
69 return XmlSerializer.serialize(this);
73 public void loadState(Element state) {
75 Element courseElement = getChildWithName(state, COURSE).getChild(COURSE_TITLED);
76 for (Element lesson : getChildList(courseElement, LESSONS, true)) {
77 int lessonIndex = getAsInt(lesson, INDEX);
78 for (Element task : getChildList(lesson, TASK_LIST, true)) {
79 int taskIndex = getAsInt(task, INDEX);
80 Map<String, Element> taskFiles = getChildMap(task, TASK_FILES, true);
81 for (Map.Entry<String, Element> entry : taskFiles.entrySet()) {
82 Element taskFileElement = entry.getValue();
83 String name = entry.getKey();
84 String answerName = FileUtil.getNameWithoutExtension(name) + CCUtils.ANSWER_EXTENSION_DOTTED + FileUtilRt.getExtension(name);
85 Document document = StudyUtils.getDocument(myProject.getBasePath(), lessonIndex, taskIndex, answerName);
86 if (document == null) {
89 for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS, true)) {
90 Element lineElement = getChildWithName(placeholder, LINE, true);
91 int line = lineElement != null ? Integer.valueOf(lineElement.getAttributeValue(VALUE)) : 0;
92 Element startElement = getChildWithName(placeholder, START, true);
93 int start = startElement != null ? Integer.valueOf(startElement.getAttributeValue(VALUE)) : 0;
94 int offset = document.getLineStartOffset(line) + start;
95 addChildWithName(placeholder, OFFSET, offset);
96 addChildWithName(placeholder, "useLength", "false");
101 XmlSerializer.deserializeInto(this, state);
102 } catch (StudyUnrecognizedFormatException e) {
107 public static CCProjectService getInstance(@NotNull Project project) {
108 return ServiceManager.getService(project, CCProjectService.class);