/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.ocelotds.web.rest;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.ws.rs.Path;
import org.ocelotds.OcelotServices;
import org.ocelotds.annotations.DataService;
import org.ocelotds.annotations.OcelotResource;

/**
 *
 * @author hhfrancois
 */
@Path("services.js")
@RequestScoped
@OcelotResource
public class RsJsServices extends AbstractRsJs {

	@Any
	@Inject
	@DataService(resolver = "")
	private Instance<Object> dataservices;

	@Override
	List<InputStream> getStreams() {
		List<InputStream> streams = new ArrayList<>();
		for (Object dataservice : dataservices) {
			addStream(streams, getJsFilename(getClassnameFromProxy(dataservice)));
		}
		return streams;
	}

	/**
	 * Return classname from proxy instance
	 *
	 * @param dataservice
	 * @return
	 */
	String getClassnameFromProxy(Object dataservice) {
		String ds = dataservice.toString();
		return getClassnameFromProxyname(ds);
	}

	/**
	 * Return classname from proxyname
	 *
	 * @param dataservice
	 * @return
	 */
	String getClassnameFromProxyname(String proxyname) {
		Pattern pattern = Pattern.compile("[@$]");
		Matcher matcher = pattern.matcher(proxyname);
		matcher.find();
		matcher.start();
		return proxyname.substring(0, matcher.start());
	}
}

@@ -550,16 +550,82 @@
 s.Path;%0A
+import javax.ws.rs.core.Context;%0Aimport javax.ws.rs.core.UriInfo;%0A
 import o
@@ -652,16 +652,16 @@
 rvices;%0A
-
 import o
@@ -786,24 +786,27 @@
 %0A@Path(%22
+%7Ba:
 services
 .js%22)%0A@R
@@ -797,16 +797,29 @@
 services
+%7Cservices.ng%7D
 .js%22)%0A@R
@@ -897,16 +897,53 @@
 RsJs %7B%0A%0A
+%09@Context%0A%09private UriInfo context;%0A%0A
 %09@Any%0A%09@
@@ -1064,16 +1064,107 @@
 ams() %7B%0A
+%09%09String fwk = null;%0A%09%09if(%22services.ng.js%22.equals(context.getPath())) %7B%0A%09%09%09fwk = %22ng%22;%0A%09%09%7D%0A
 %09%09List%3CI
@@ -1248,16 +1248,73 @@
 ices) %7B%0A
+%09%09%09if (!OcelotServices.class.isInstance(dataservice)) %7B%0A%09
 %09%09%09addSt
@@ -1375,19 +1375,29 @@
 service)
-));
+, fwk));%0A%09%09%09%7D
 %0A%09%09%7D%0A%09%09r

allow download services.ng.js