88import fi .helsinki .cs .tmc .cli .io .TmcCliProgressObserver ;
99import fi .helsinki .cs .tmc .cli .tmcstuff .CourseInfo ;
1010import fi .helsinki .cs .tmc .cli .tmcstuff .CourseInfoIo ;
11+ import fi .helsinki .cs .tmc .cli .tmcstuff .Settings ;
12+ import fi .helsinki .cs .tmc .cli .tmcstuff .SettingsIo ;
1113import fi .helsinki .cs .tmc .cli .tmcstuff .TmcUtil ;
1214import fi .helsinki .cs .tmc .cli .tmcstuff .WorkDir ;
1315import fi .helsinki .cs .tmc .core .domain .Course ;
2325@ Command (name = "download" , desc = "Download exercises for a specific course" )
2426public class DownloadExercisesCommand extends AbstractCommand {
2527
28+ private CliContext ctx ;
2629 private boolean showAll ;
2730
2831 @ Override
@@ -43,23 +46,22 @@ public void run(CommandLine args, Io io) {
4346 return ;
4447 }
4548
49+ ctx = getContext ();
4650 showAll = args .hasOption ("a" );
4751
48- CliContext ctx = getContext ();
49- WorkDir workDir = ctx .getWorkDir ();
5052 if (!ctx .loadBackend ()) {
5153 return ;
5254 }
5355
56+ WorkDir workDir = ctx .getWorkDir ();
5457 if (workDir .getConfigFile () != null ) {
5558 io .println ("Can't download a course inside a course directory." );
5659 return ;
5760 }
5861
5962 String courseName = stringArgs [0 ];
60- Course course = TmcUtil . findCourse (ctx , courseName );
63+ Course course = findCourse (courseName );
6164 if (course == null ) {
62- io .println ("Course doesn't exist." );
6365 return ;
6466 }
6567 List <Exercise > filtered = getFilteredExercises (course );
@@ -76,47 +78,91 @@ public void run(CommandLine args, Io io) {
7678 return ;
7779 }
7880
81+ printStatistics (course , filtered .size (), exercises .size ());
82+ createNewCourse (course );
83+ }
84+
85+ // TODO This method could be moved somewhere else.
86+ private Course findCourse (String courseName ) {
87+ Io io = ctx .getIo ();
88+ Course found = null ;
89+ boolean hasDuplicateNames = false ;
90+
91+ List <Settings > accountsList = SettingsIo .getSettingsList ();
92+
93+ for (Settings settings : accountsList ) {
94+ ctx .useSettings (settings );
95+ Course course = TmcUtil .findCourse (ctx , courseName );
96+ if (course == null ) {
97+ continue ;
98+ }
99+ if (found != null ) {
100+ if (!hasDuplicateNames ) {
101+ io .println ("There is multiple courses with same name at different servers." );
102+ }
103+ if (io .readConfirmation ("Download course from "
104+ + settings .getServerAddress (), false )) {
105+ return course ;
106+ }
107+ hasDuplicateNames = true ;
108+ }
109+ found = course ;
110+ }
111+ if (found == null ) {
112+ io .println ("Course doesn't exist." );
113+ return null ;
114+ }
115+ return found ;
116+ }
117+
118+ private List <Exercise > getFilteredExercises (Course course ) {
119+ if (showAll ) {
120+ return course .getExercises ();
121+ }
122+
123+ List <Exercise > filtered = new ArrayList <>();
124+ for (Exercise exercise : course .getExercises ()) {
125+ // Teachers may get a list of locked exercises but core still refuses to
126+ // download them. Filter locked exercises out.
127+ if (!exercise .isCompleted () && !exercise .isLocked ()) {
128+ filtered .add (exercise );
129+ }
130+ }
131+ return filtered ;
132+ }
133+
134+ private void printStatistics (Course course , int requestCount , int downloadCount ) {
135+ Io io = ctx .getIo ();
136+ String courseName = course .getName ();
79137 if (course .getExercises ().isEmpty ()) {
80138 io .println ("The '" + courseName + "' course doesn't have any exercises." );
81139 } else {
82140 io .println ("The '" + courseName + "' course has "
83141 + course .getExercises ().size () + " exercises" );
84142
85- int failedCount = (filtered . size () - exercises . size () );
143+ int failedCount = (requestCount - downloadCount );
86144 if (failedCount > 0 ) {
87- io .println (" from which "
88- + exercises . size () + " exercises were succesfully downloaded" );
145+ io .println (" from which " + ( requestCount - failedCount )
146+ + " exercises were succesfully downloaded" );
89147 io .println (Color .colorString (" and of which " + failedCount + " failed." ,
90148 Color .AnsiColor .ANSI_RED ));
91149 //TODO we could print the names of the not downloaded exercises here
92150 } else {
93151 io .println (" from which "
94- + exercises . size () + " exercises were downloaded." );
152+ + downloadCount + " exercises were downloaded." );
95153 }
96154 io .println ("Use -a/--all to download completed exercises as well." );
97155 }
156+ }
98157
158+ private void createNewCourse (Course course ) {
159+ WorkDir workDir = ctx .getWorkDir ();
99160 Path configFile = workDir .getWorkingDirectory ()
100- .resolve (courseName )
161+ .resolve (course . getName () )
101162 .resolve (CourseInfoIo .COURSE_CONFIG );
163+
102164 CourseInfo info = ctx .createCourseInfo (course );
103165 info .setExercises (course .getExercises ());
104166 CourseInfoIo .save (info , configFile );
105167 }
106-
107- private List <Exercise > getFilteredExercises (Course course ) {
108- if (showAll ) {
109- return course .getExercises ();
110- }
111-
112- List <Exercise > filtered = new ArrayList <>();
113- for (Exercise exercise : course .getExercises ()) {
114- // Teachers may get a list of locked exercises but core still refuses to
115- // download them. Filter locked exercises out.
116- if (!exercise .isCompleted () && !exercise .isLocked ()) {
117- filtered .add (exercise );
118- }
119- }
120- return filtered ;
121- }
122168}
0 commit comments