/**
 *  Copyright (c) 2012, 2016 Sme.UP and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *
 * Contributors:
 *   Franco Lombardo - Initial API and implementation
 */
package org.smeup.sys.os.dtaara.base.api;

import javax.inject.Inject;

import org.smeup.sys.dk.core.annotation.ToDo;
import org.smeup.sys.il.data.QAdapter;
import org.smeup.sys.il.data.QCharacter;
import org.smeup.sys.il.data.QDataContext;
import org.smeup.sys.il.data.annotation.Main;
import org.smeup.sys.il.data.annotation.Program;
import org.smeup.sys.il.memo.QResourceManager;
import org.smeup.sys.os.core.QExceptionManager;
import org.smeup.sys.os.core.jobs.QJob;
import org.smeup.sys.os.dtaara.QDataArea;
import org.smeup.sys.os.dtaara.QDataAreaManager;
import org.smeup.sys.os.dtaara.base.api.tools.DataAreaSpecification;
import org.smeup.sys.os.dtaara.base.api.tools.DataAreaSpecification.SUBSTRINGSPECIFICATIONS;
import org.smeup.sys.os.dtaara.base.api.tools.ExistingDataAreaSpecification.DataAreaNotFoundException;
import org.smeup.sys.os.dtaara.base.api.tools.ExistingDataAreaSpecification.LibraryNotFoundException;

@Program(name = "QCLRTVDA")
public @ToDo class DataAreaRetriever {
	public static enum QCPFMSG {
		CPF1015, // Non è stata trovata l'area dati &1 in &2.
		CPF1021, // Non è stata trovata la libreria &1 per l'area dati &2.
		CPF9899, // Si è verificato un errore durante l'elaborazione del
					// comando.
	}

	@Inject
	private QResourceManager resourceManager;
	@Inject
	private QJob job;
	@Inject
	private QDataAreaManager dataAreaManager;
	@Inject
	private QExceptionManager exceptionManager;
	@Inject
	private QDataContext dataContext;

	@Main
	public void main(DataAreaSpecification dataAreaParm, QAdapter cLVariableForReturnedValue) {
		try {
			QDataArea area = dataAreaParm.dataAreaSpecification.asData().findDataArea(job, resourceManager, dataAreaManager, dataAreaParm.dataAreaSpecification.asEnum());

			QCharacter character = dataContext.getDataFactory().createCharacter(area.getContent().length(), false, false);
			if (dataAreaParm.all()) {
				cLVariableForReturnedValue.slice(character, 1);
			} else {
				SUBSTRINGSPECIFICATIONS substringSpec = dataAreaParm.substringSpecifications.asData();
				cLVariableForReturnedValue.slice(character, substringSpec.substringStartingPosition.asInteger());
			}

		} catch (DataAreaNotFoundException e) {
			throw exceptionManager.prepareException(job, QCPFMSG.CPF1015, new String[] { e.dataAreaName, e.libraryName });
		} catch (LibraryNotFoundException e) {
			throw exceptionManager.prepareException(job, QCPFMSG.CPF1021, new String[] { e.libraryName, e.dataAreaName });
		} catch (RuntimeException e) {
			throw exceptionManager.prepareException(job, QCPFMSG.CPF9899, new String[0]);
		}
	}

}

@@ -537,92 +537,8 @@
 er;%0A
-import org.smeup.sys.il.data.QCharacter;%0Aimport org.smeup.sys.il.data.QDataContext;%0A
 impo
@@ -1721,52 +1721,8 @@
 ger;
-%0A%09@Inject%0A%09private QDataContext dataContext;
 %0A%0A%09@
@@ -1994,122 +1994,8 @@
 );%0A%0A
-%09%09%09QCharacter character = dataContext.getDataFactory().createCharacter(area.getContent().length(), false, false);%0A
 %09%09%09i
@@ -2054,26 +2054,31 @@
 lue.
-slice(character, 1
+movel(area.getContent()
 );%0A%09
@@ -2214,25 +2214,42 @@
 lue.
-slice(character, 
+movel(area.getContent().substring(
 subs
@@ -2295,16 +2295,62 @@
 nteger()
+-1, substringSpec.substringLength.asInteger())
 );%0A%09%09%09%7D%0A

Patch dataAreaRetriever