|  | @@ 120-123 (lines=4) @@ | 
                                                            
                                    | 117 |  | 	 */ | 
                                                            
                                    | 118 |  | 	public function getPaperHiveBookURL($dir, $filename) { | 
                                                            
                                    | 119 |  | 		$bookId = $this->getBookIdforPath($dir, $filename); | 
                                                            
                                    | 120 |  | 		if (!$bookId) { | 
                                                            
                                    | 121 |  | 			$message = (string)$this->l->t('No such document found in database.'); | 
                                                            
                                    | 122 |  | 			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); | 
                                                            
                                    | 123 |  | 		} | 
                                                            
                                    | 124 |  | 		return new DataResponse($this->paperhive_base_url . $this->paperhive_base_document_url . $bookId, Http::STATUS_OK); | 
                                                            
                                    | 125 |  | 	} | 
                                                            
                                    | 126 |  |  | 
                                                                                
                                |  | @@ 212-215 (lines=4) @@ | 
                                                            
                                    | 209 |  | 	public function generatePaperHiveDocument($dir, $bookID) { | 
                                                            
                                    | 210 |  | 		// Try to get the document | 
                                                            
                                    | 211 |  | 		$paperHiveObjectString = $this->fetchDocument($bookID); | 
                                                            
                                    | 212 |  | 		if ($paperHiveObjectString === false) { | 
                                                            
                                    | 213 |  | 			$message = (string)$this->l->t('Problem connecting to PaperHive.'); | 
                                                            
                                    | 214 |  | 			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); | 
                                                            
                                    | 215 |  | 		} | 
                                                            
                                    | 216 |  | 		$paperHiveObject = json_decode($paperHiveObjectString, true); | 
                                                            
                                    | 217 |  |  | 
                                                            
                                    | 218 |  | 		// Check if correct response has been returned | 
                                                                                
                                |  | @@ 219-222 (lines=4) @@ | 
                                                            
                                    | 216 |  | 		$paperHiveObject = json_decode($paperHiveObjectString, true); | 
                                                            
                                    | 217 |  |  | 
                                                            
                                    | 218 |  | 		// Check if correct response has been returned | 
                                                            
                                    | 219 |  | 		if (json_last_error() != JSON_ERROR_NONE) { | 
                                                            
                                    | 220 |  | 			$message = (string)$this->l->t('Received wrong response from PaperHive.'); | 
                                                            
                                    | 221 |  | 			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); | 
                                                            
                                    | 222 |  | 		} | 
                                                            
                                    | 223 |  |  | 
                                                            
                                    | 224 |  | 		// Check if document is found | 
                                                            
                                    | 225 |  | 		if (!(isset($paperHiveObject['metadata']) && isset($paperHiveObject['metadata']['title']))) { | 
                                                                                
                                |  | @@ 233-236 (lines=4) @@ | 
                                                            
                                    | 230 |  | 		// Try fetching discussions | 
                                                            
                                    | 231 |  | 		$paperHiveDiscussionsString = $this->fetchDiscussions($bookID); | 
                                                            
                                    | 232 |  | 		$paperHiveDiscussions = json_decode($paperHiveDiscussionsString, true); | 
                                                            
                                    | 233 |  | 		if ($paperHiveDiscussionsString === false || json_last_error() != JSON_ERROR_NONE) { | 
                                                            
                                    | 234 |  | 			$message = (string)$this->l->t('Problem connecting to PaperHive to fetch discussions.'); | 
                                                            
                                    | 235 |  | 			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); | 
                                                            
                                    | 236 |  | 		} | 
                                                            
                                    | 237 |  | 		$discussionCount = -1; | 
                                                            
                                    | 238 |  | 		if (json_last_error() === JSON_ERROR_NONE && isset($paperHiveDiscussions['discussions'])) { | 
                                                            
                                    | 239 |  | 			$discussionCount = count($paperHiveDiscussions['discussions']); | 
                                                                                
                                |  | @@ 252-255 (lines=4) @@ | 
                                                            
                                    | 249 |  | 		} | 
                                                            
                                    | 250 |  |  | 
                                                            
                                    | 251 |  | 		$exists = $this->view->file_exists($path); | 
                                                            
                                    | 252 |  | 		if ($exists) { | 
                                                            
                                    | 253 |  | 			$message = (string) $this->l->t('The file already exists.'); | 
                                                            
                                    | 254 |  | 			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); | 
                                                            
                                    | 255 |  | 		} | 
                                                            
                                    | 256 |  |  | 
                                                            
                                    | 257 |  | 		try { | 
                                                            
                                    | 258 |  | 			$created = $this->view->touch($path); | 
                                                                                
                                |  | @@ 259-262 (lines=4) @@ | 
                                                            
                                    | 256 |  |  | 
                                                            
                                    | 257 |  | 		try { | 
                                                            
                                    | 258 |  | 			$created = $this->view->touch($path); | 
                                                            
                                    | 259 |  | 			if(!$created) { | 
                                                            
                                    | 260 |  | 				$message = (string) $this->l->t('Could not save document.'); | 
                                                            
                                    | 261 |  | 				return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); | 
                                                            
                                    | 262 |  | 			} | 
                                                            
                                    | 263 |  |  | 
                                                            
                                    | 264 |  | 			$fileInfo = $this->view->getFileInfo($path); | 
                                                            
                                    | 265 |  | 			$inserted = $this->paperHiveMetadata->insertBookID($fileInfo['fileid'], $bookID); | 
                                                                                
                                |  | @@ 266-270 (lines=5) @@ | 
                                                            
                                    | 263 |  |  | 
                                                            
                                    | 264 |  | 			$fileInfo = $this->view->getFileInfo($path); | 
                                                            
                                    | 265 |  | 			$inserted = $this->paperHiveMetadata->insertBookID($fileInfo['fileid'], $bookID); | 
                                                            
                                    | 266 |  | 			if(!$inserted) { | 
                                                            
                                    | 267 |  | 				$this->view->unlink($path); | 
                                                            
                                    | 268 |  | 				$message = (string) $this->l->t('Could not save document metadata.'); | 
                                                            
                                    | 269 |  | 				return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); | 
                                                            
                                    | 270 |  | 			} | 
                                                            
                                    | 271 |  | 		} catch (LockedException $e) { | 
                                                            
                                    | 272 |  | 			$message = (string) $this->l->t('The file is locked.'); | 
                                                            
                                    | 273 |  | 			return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |